summaryrefslogtreecommitdiffstats
path: root/kate/filelistloader/katefll_plugin.cpp
blob: deec0213c49d3ed76632722df63a7bb3e88f2d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************
                          katefll_plugin.cpp  -  description
                             -------------------
    begin                : FRE July 12th 2002
    copyright            : (C) 2002 by Joseph Wenninger
    email                : jowenn@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "katefll_plugin.h"
#include "katefll_plugin.moc"

#include <kate/pluginmanager.h>

#include <tqfileinfo.h>
#include <kgenericfactory.h>
#include <kaction.h>
#include <klocale.h>
#include <kdebug.h>
#include <kurl.h>
#include <kio/netaccess.h>
#include <kfiledialog.h>
#include <ktempfile.h>
#include <kmessagebox.h>

K_EXPORT_COMPONENT_FACTORY( katefll_plugin, KGenericFactory<PluginKateFileListLoader>( "katefll_loader" ) )

class PluginView : public KXMLGUIClient
{
  friend class PluginKateFileListLoader;

  public:
    Kate::MainWindow *win;
    KRecentFilesAction *recentFiles;
};

PluginKateFileListLoader::PluginKateFileListLoader (TQObject * parent, const char *name,  const TQStringList)
  : Plugin((Kate::Application*)parent,name), PluginViewInterface(),
    m_config( new TDEConfig("katefilelistpluginrc") )
{
  m_config->setGroup("General");
  updateInit();
}


PluginKateFileListLoader:: ~PluginKateFileListLoader()
{
  m_config->sync();
  delete m_config;
}


void PluginKateFileListLoader::updateInit()
{
  /*if (application()->initPluginManager()->initPlugin())
  {
    if (application()->initPluginManager()->initPlugin()->tqt_cast("InitPluginKateFileListLoader")) //That's needed, if the plugin is loaded during a configuration change
    m_saveURL=application()->initPluginManager()->initScript();
  }
  else*/ m_saveURL=KURL();
  m_oldInitURL="";//application()->initPluginManager()->initScript();
  m_saveAs=false;
}

void PluginKateFileListLoader::addView(Kate::MainWindow *win)
{
  PluginView *view = new PluginView ();

  (void) new KAction( i18n("Open File List..."), 0,
    this, TQT_SLOT( slotOpenList() ),
    view->actionCollection(), "file_kfllopen" );

  view->recentFiles = new KRecentFilesAction( i18n("Open Recent"), KShortcut::null(),
    this, TQT_SLOT(slotOpenList(const KURL&)), view->actionCollection(),
    "file_kfllopenrecent");
  view->recentFiles->loadEntries(m_config, "Recent Files");

  (void) new KAction( i18n("Save File List"), 0,
    this, TQT_SLOT( slotSaveList() ),
    view->actionCollection(), "file_kfllsave" );

  (void) new KAction( i18n("Save File List As..."), 0,
    this, TQT_SLOT( slotSaveListAs() ),
    view->actionCollection(), "file_kfllsaveas" );

  view->setInstance (new TDEInstance("kate"));
  view->setXMLFile( "plugins/katefll/ui.rc" );
  win->guiFactory()->addClient (view);
  view->win = win;

  m_views.append (view);
}

void PluginKateFileListLoader::removeView(Kate::MainWindow *win)
{
  if (m_views.count() == 1) // yeah baby, last view! So save filelist
    m_views.at(0)->recentFiles->saveEntries(m_config, "Recent Files");

  for (uint z=0; z < m_views.count(); z++)
    if (m_views.at(z)->win == win)
    {
      PluginView *view = m_views.at(z);
      m_views.remove (view);
      win->guiFactory()->removeClient (view);
      delete view;
    }
}

void PluginKateFileListLoader::addURLToList(const KURL& url)
{
  // add url to all views to make sure all are synced
  for (uint a = 0; a < m_views.count(); ++a)
  {
    m_views.at(a)->recentFiles->addURL(url);
  }
}

void PluginKateFileListLoader::removeURLFromList(const KURL& url)
{
  // remove url from all views to make sure all are synced
  for (uint a = 0; a < m_views.count(); ++a) {
    m_views.at(a)->recentFiles->removeURL(url);
  }
}

void PluginKateFileListLoader::slotOpenList()
{
  KURL url = KFileDialog::getOpenURL(TQString(),
    "*.katefl|Kate File List (*.katefl)");
  if (url.isValid()) // cancel pressed?
    slotOpenList(url);
}

void PluginKateFileListLoader::slotOpenList(const KURL& url)
{
  if ( url.isValid() && TDEIO::NetAccess::exists(url, false, 0) )
  {
    addURLToList( url );
    if ( KMessageBox::questionYesNo (0,
	 i18n("Do you want to close all other files first?"),
	 i18n("Kate Filelist Loader"),
	 KStdGuiItem::close(), i18n("Do Not Close"),
	 "kate-filelist-loader-close-other" ) == KMessageBox::Yes )
      application()->documentManager()->closeAllDocuments();

    TQString tmpFile;
    if( TDEIO::NetAccess::download( url, tmpFile, 0 ) )
    {
      TQFile file(tmpFile);
      file.open(IO_ReadOnly);

      TQTextStream t(&file);
      KURL tmp;
      while (!t.eof()) {
	if ( ! tmp.isEmpty() )
	  application()->documentManager()->openURL( tmp );

	tmp = KURL(t.readLine());
//         application()->activeMainWindow()->viewManager()->openURL(KURL(t.readLine()));
      }

      file.close();
      if ( ! tmp.isEmpty() )
         application()->activeMainWindow()->viewManager()->openURL(tmp);

      TDEIO::NetAccess::removeTempFile( tmpFile );
    } else application()->documentManager()->openURL(KURL());
  }
  else
  {
    KMessageBox::error(0, i18n("The selected filelist does not exist or is invalid."));
    // url not valid -> remove from list
    removeURLFromList(url);
  }
}

void PluginKateFileListLoader::slotSaveList()
{
  if (m_saveURL.isValid())
  {
    if (m_saveAs)
    {
    /*  if (m_oldInitURL!=application()->initPluginManager()->initScript())
      {
	switch (KMessageBox::questionYesNoCancel(0,i18n("<qt>Since the last time you saved the file list, Kate has been reinitialized by another plugin other than the <B>File List Loader</B>. Do you still want to save the list to %1?</qt>").arg(m_saveURL.prettyURL()),TQString(),KStdGuiItem::save(),KStdGuiItem::discard()))
        {
          case KMessageBox::Yes: save(); break;
          case KMessageBox::No: slotSaveListAs(); break;
          default: break;
        }
      }
      else*/ save();
    }
    else
    {
      /*if (m_saveURL!=application()->initPluginManager()->initScript())
      {
	switch (KMessageBox::questionYesNoCancel(0,i18n("<qt>Kate has been reinitialized by another plugin other than the <B>File List Loader</B>. Do you still want to save the list to %1?</qt>").arg(m_saveURL.prettyURL()),TQString(),KStdGuiItem::save(),KStdGuiItem::discard()))
        {
          case KMessageBox::Yes: save(); break;
          case KMessageBox::No: slotSaveListAs(); break;
          default: break;
        }
      }
      else */save();
    }
  } else slotSaveListAs();
}

void PluginKateFileListLoader::save()
{
  KTempFile file(locateLocal("tmp", "kate"), "katefll");
  for (uint i=0;i<application()->documentManager()->documents();i++)
  {
    *file.textStream()<<application()->documentManager()->document(i)->url().url()<<endl;
  }
  file.close();
  TDEIO::NetAccess::upload(file.name(), m_saveURL, 0);
  file.unlink();
  addURLToList( m_saveURL );
}

void PluginKateFileListLoader::slotSaveListAs()
{
  KURL url=KFileDialog::getSaveURL(TQString(),"*.katefl|Kate File List (*.katefl)");
  if (url.isValid())
  {
    m_oldInitURL="";//application()->initPluginManager()->initScript();
    m_saveURL=url;
    m_saveAs=true;
    save();
  }
}