• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • app
kateapp.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
3  Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kateapp.h"
21 #include "kateapp.moc"
22 
23 #include "katedocmanager.h"
24 #include "katepluginmanager.h"
25 #include "kateviewmanager.h"
26 #include "kateappIface.h"
27 #include "katesession.h"
28 #include "katemainwindow.h"
29 
30 #include "../interfaces/application.h"
31 
32 #include <tdecmdlineargs.h>
33 #include <dcopclient.h>
34 #include <tdeconfig.h>
35 #include <twin.h>
36 #include <ktip.h>
37 #include <kdebug.h>
38 #include <klibloader.h>
39 #include <tdemessagebox.h>
40 #include <tdelocale.h>
41 #include <tdesimpleconfig.h>
42 #include <tdestartupinfo.h>
43 
44 #include <tqfile.h>
45 #include <tqtimer.h>
46 #include <tqdir.h>
47 #include <tqtextcodec.h>
48 
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <sys/types.h>
52 
53 KateApp::KateApp (TDECmdLineArgs *args)
54  : TDEApplication ()
55  , m_args (args)
56  , m_shouldExit (false)
57 {
58  // Don't handle DCOP requests yet
59  dcopClient()->suspend();
60 
61  // insert right translations for the katepart
62  TDEGlobal::locale()->insertCatalogue("katepart");
63 
64  // some global default
65  Kate::Document::setFileChangedDialogsActivated (true);
66 
67  // application interface
68  m_application = new Kate::Application (this);
69 
70  // doc + project man
71  m_docManager = new KateDocManager (this);
72 
73  // init all normal plugins
74  m_pluginManager = new KatePluginManager (this);
75 
76  // session manager up
77  m_sessionManager = KateSessionManager::self();
78 
79  // application dcop interface
80  m_obj = new KateAppDCOPIface (this);
81 
82  kdDebug()<<"Setting KATE_PID: '"<<getpid()<<"'"<<endl;
83  ::setenv( "KATE_PID", TQString(TQString("%1").arg(getpid())).latin1(), 1 );
84 
85  // handle restore different
86  if (isRestored())
87  {
88  restoreKate ();
89  }
90  else
91  {
92  // let us handle our command line args and co ;)
93  // we can exit here if session chooser decides
94  if (!startupKate ())
95  {
96  m_shouldExit = true;
97  return;
98  }
99  }
100 
101  // Ok. We are ready for DCOP requests.
102  dcopClient()->resume();
103 }
104 
105 KateApp::~KateApp ()
106 {
107  delete m_obj; // cu dcop interface
108  delete m_pluginManager; // cu plugin manager
109  delete m_sessionManager; // delete session manager
110  delete m_docManager; // delete document manager. Do this now, or we crash
111 }
112 
113 KateApp *KateApp::self ()
114 {
115  return (KateApp *) tdeApp;
116 }
117 
118 Kate::Application *KateApp::application ()
119 {
120  return m_application;
121 }
122 
127 TQString KateApp::kateVersion (bool fullVersion)
128 {
129 // return fullVersion ? TQString ("%1.%2.%3").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()).arg(KDE::versionRelease())
130 // : TQString ("%1.%2").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor());
134  return fullVersion ? TQString ("2.5.%1").arg(KDE::versionMajor()) : TQString ("%1.%2").arg(2.5);
135 }
136 
137 void KateApp::restoreKate()
138 {
139  // restore the nice files ;) we need it
140  Kate::Document::setOpenErrorDialogsActivated(false);
141 
142  // restore last session
143  sessionManager()->restoreLastSession();
144  m_docManager->restoreDocumentList(sessionConfig());
145 
146  Kate::Document::setOpenErrorDialogsActivated(true);
147 
148  // no mainwindow, create one, should not happen, but make sure ;)
149  if (mainWindows() == 0)
150  newMainWindow();
151 
152  // Do not notify about start there: this makes kicker crazy and kate go to a wrong desktop.
153  // TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId());
154 }
155 
156 bool KateApp::startupKate()
157 {
158  if (m_args->isSet("start"))
159  {
160  // the user has specified the session to open. If the session does not exist,
161  // a new session with the specified name will be created
162  TQCString sessName = m_args->getOption("start");
163  int sessId = sessionManager()->getSessionIdFromName(sessName);
164  if (sessId != KateSessionManager::INVALID_SESSION)
165  {
166  sessionManager()->activateSession(sessId);
167  }
168  else
169  {
170  sessionManager()->newSession(sessName);
171  }
172  }
173  else
174  {
175  // check Kate session startup options
176  int startupOption = sessionManager()->getStartupOption();
177  if (startupOption == KateSessionManager::STARTUP_NEW)
178  {
179  sessionManager()->newSession();
180  }
181  else if (startupOption == KateSessionManager::STARTUP_LAST)
182  {
183  sessionManager()->restoreLastSession();
184  }
185  else // startupOption == KateSessionManager::STARTUP_MANUAL
186  {
187  KateSessionChooser *chooser = new KateSessionChooser(NULL);
188  int result = chooser->exec();
189  switch (result)
190  {
191  case KateSessionChooser::RESULT_OPEN_NEW:
192  sessionManager()->newSession();
193  break;
194 
195  case KateSessionChooser::RESULT_OPEN_EXISTING:
196  if (!m_sessionManager->activateSession(chooser->getSelectedSessionId()))
197  {
198  // Open a new session in case of error
199  sessionManager()->newSession();
200  }
201  break;
202 
203  default: // KateSessionChooser::RESULT_QUIT_KATE:
204  // Kate will exit now and notify it is done
205  TDEStartupInfo::appStarted(startupId());
206  return false;
207  break;
208  }
209  delete chooser;
210  }
211  }
212 
213  // oh, no mainwindow, create one, should not happen, but make sure ;)
214  if (mainWindows() == 0)
215  newMainWindow ();
216 
217  // notify about start
218  TDEStartupInfo::setNewStartupId( activeMainWindow(), startupId());
219 
220  TQTextCodec *codec = m_args->isSet("encoding") ? TQTextCodec::codecForName(m_args->getOption("encoding")) : 0;
221 
222  bool tempfileSet = TDECmdLineArgs::isTempFileSet();
223 
224  Kate::Document::setOpenErrorDialogsActivated (false);
225  uint id = 0;
226  for (int z=0; z<m_args->count(); z++)
227  {
228  // this file is no local dir, open it, else warn
229  bool noDir = !m_args->url(z).isLocalFile() || !TQDir (m_args->url(z).path()).exists();
230 
231  if (noDir)
232  {
233  // open a normal file
234  if (codec)
235  id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false, tempfileSet );
236  else
237  id = activeMainWindow()->viewManager()->openURL( m_args->url(z), TQString::null, false, tempfileSet );
238  }
239  else
240  KMessageBox::sorry( activeMainWindow(),
241  i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).pathOrURL()) );
242  }
243 
244  Kate::Document::setOpenErrorDialogsActivated (true);
245 
246  // handle stdin input
247  if( m_args->isSet( "stdin" ) )
248  {
249  TQTextIStream input(stdin);
250 
251  // set chosen codec
252  if (codec)
253  input.setCodec (codec);
254 
255  TQString line;
256  TQString text;
257 
258  do
259  {
260  line = input.readLine();
261  text.append( line + "\n" );
262  } while( !line.isNull() );
263 
264  openInput (text);
265  }
266  else if ( id )
267  activeMainWindow()->viewManager()->activateView( id );
268 
269  if ( activeMainWindow()->viewManager()->viewCount () == 0 )
270  activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber());
271 
272  int line = 0;
273  int column = 0;
274  bool nav = false;
275 
276  if (m_args->isSet ("line"))
277  {
278  line = m_args->getOption ("line").toInt() - 1;
279  if (line < 0)
280  {
281  line = 0;
282  }
283  nav = true;
284  }
285 
286  if (m_args->isSet ("column"))
287  {
288  column = m_args->getOption ("column").toInt() - 1;
289  if (column < 0)
290  {
291  column = 0;
292  }
293  nav = true;
294  }
295 
296  if (nav)
297  activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column);
298 
299  // show the nice tips
300  KTipDialog::showTip(activeMainWindow());
301 
302  return true;
303 }
304 
305 void KateApp::shutdownKate(KateMainWindow *win)
306 {
307  if (!win->queryClose_internal())
308  return;
309 
310  // detach the dcopClient
311  dcopClient()->detach();
312 
313  // cu main windows
314  while (!m_mainWindows.isEmpty())
315  delete m_mainWindows[0];
316 
317  quit();
318 }
319 
320 bool KateApp::query_session_close()
321 {
322  bool saveSessions = false;
323  int switchOption = m_sessionManager->getSwitchOption();
324  if (switchOption == KateSessionManager::SWITCH_SAVE)
325  {
326  saveSessions = true;
327  }
328  else if (switchOption == KateSessionManager::SWITCH_ASK)
329  {
330  KDialogBase *dlg = new KDialogBase(i18n("Save Sessions"),
331  KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
332  KDialogBase::Cancel, KDialogBase::Cancel, NULL, NULL, true, false,
333  KStdGuiItem::save(), KStdGuiItem::del(), KStdGuiItem::cancel());
334  bool dontAgain = false;
335  int res = KMessageBox::createKMessageBox(dlg, TQMessageBox::Warning,
336  i18n("<p>Do you want to save the existing sessions?<p>!!NOTE!!"
337  "<p>All existing sessions will be removed "
338  "if you choose \"Delete\""), TQStringList(),
339  i18n("Do not ask again"), &dontAgain, KMessageBox::Notify);
340  if (res == KDialogBase::Cancel)
341  {
342  return false;
343  }
344  if (dontAgain)
345  {
346  if (res == KDialogBase::No)
347  {
348  m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_DISCARD);
349  }
350  else
351  {
352  m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_SAVE);
353  }
354  }
355  if (res == KDialogBase::Yes)
356  {
357  saveSessions = true;
358  }
359  }
360 
361  if (saveSessions)
362  {
363  m_sessionManager->saveActiveSession();
364  }
365  m_sessionManager->saveConfig(saveSessions);
366  return true;
367 }
368 
369 void KateApp::reparse_config()
370 {
371  emit optionsChanged();
372 }
373 
374 KatePluginManager *KateApp::pluginManager()
375 {
376  return m_pluginManager;
377 }
378 
379 KateDocManager *KateApp::documentManager ()
380 {
381  return m_docManager;
382 }
383 
384 KateSessionManager* KateApp::sessionManager()
385 {
386  return m_sessionManager;
387 }
388 
389 bool KateApp::openURL (const KURL &url, const TQString &encoding, bool isTempFile)
390 {
391  KateMainWindow *mainWindow = activeMainWindow ();
392 
393  if (!mainWindow)
394  return false;
395 
396  TQTextCodec *codec = encoding.isEmpty() ? 0 : TQTextCodec::codecForName(encoding.latin1());
397 
398  kdDebug () << "OPEN URL "<< encoding << endl;
399 
400  // this file is no local dir, open it, else warn
401  bool noDir = !url.isLocalFile() || !TQDir (url.path()).exists();
402 
403  if (noDir)
404  {
405  // open a normal file
406  if (codec)
407  mainWindow->viewManager()->openURL( url, codec->name(), true, isTempFile );
408  else
409  mainWindow->viewManager()->openURL( url, TQString::null, true, isTempFile );
410  }
411  else
412  KMessageBox::sorry( mainWindow,
413  i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.pathOrURL()) );
414 
415  return true;
416 }
417 
418 bool KateApp::setCursor (int line, int column)
419 {
420  KateMainWindow *mainWindow = activeMainWindow ();
421 
422  if (!mainWindow)
423  return false;
424 
425  mainWindow->viewManager()->activeView ()->setCursorPosition (line, column);
426 
427  return true;
428 }
429 
430 bool KateApp::openInput (const TQString &text)
431 {
432  activeMainWindow()->viewManager()->openURL( "", "", true );
433 
434  if (!activeMainWindow()->viewManager()->activeView ())
435  return false;
436 
437  activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text);
438 
439  return true;
440 }
441 
442 KateMainWindow *KateApp::newMainWindow (TDEConfig *sconfig, const TQString &sgroup)
443 {
444  KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup);
445  m_mainWindows.push_back (mainWindow);
446 
447  if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView())
448  mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() );
449  else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
450  mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
451  else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
452  mainWindow->viewManager()->openURL ( KURL() );
453 
454  mainWindow->show ();
455 
456  return mainWindow;
457 }
458 
459 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
460 {
461  m_mainWindows.remove (mainWindow);
462 }
463 
464 KateMainWindow *KateApp::activeMainWindow ()
465 {
466  if (m_mainWindows.isEmpty())
467  return 0;
468 
469  int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow());
470 
471  if (n < 0)
472  n=0;
473 
474  return m_mainWindows[n];
475 }
476 
477 uint KateApp::mainWindows () const
478 {
479  return m_mainWindows.size();
480 }
481 
482 KateMainWindow *KateApp::mainWindow (uint n)
483 {
484  if (n < m_mainWindows.size())
485  return m_mainWindows[n];
486 
487  return 0;
488 }
KateApp
Kate Application This class represents the core kate application object.
Definition: kateapp.h:43
KateApp::query_session_close
bool query_session_close()
to be called when the application is about to quit
Definition: kateapp.cpp:320
KateApp::optionsChanged
void optionsChanged()
Emitted when the configuration has or may have been changed.
KateApp::reparse_config
void reparse_config()
called after the config dialog has been closed.
Definition: kateapp.cpp:369
KateApp::application
Kate::Application * application()
accessor to the Kate::Application plugin interface
Definition: kateapp.cpp:118
KateApp::newMainWindow
KateMainWindow * newMainWindow(TDEConfig *sconfig=0, const TQString &sgroup="")
window management
Definition: kateapp.cpp:442
KateApp::shutdownKate
void shutdownKate(KateMainWindow *win)
kate shutdown
Definition: kateapp.cpp:305
KateApp::mainWindow
KateMainWindow * mainWindow(uint n)
give back the window you want
Definition: kateapp.cpp:482
KateApp::~KateApp
~KateApp()
application destructor
Definition: kateapp.cpp:105
KateApp::self
static KateApp * self()
static accessor to avoid casting ;)
Definition: kateapp.cpp:113
KateApp::documentManager
KateDocManager * documentManager()
accessor to document manager
Definition: kateapp.cpp:379
KateApp::kateVersion
static TQString kateVersion(bool fullVersion=true)
Returns the current Kate version (X.Y) or (X.Y.Z)
Definition: kateapp.cpp:127
KateApp::activeMainWindow
KateMainWindow * activeMainWindow()
give back current active main window can only be 0 at app start or exit
Definition: kateapp.cpp:464
KateApp::pluginManager
KatePluginManager * pluginManager()
other accessors for global unique instances
Definition: kateapp.cpp:374
KateApp::openURL
bool openURL(const KURL &url, const TQString &encoding, bool isTempFile)
some stuff for the dcop API
Definition: kateapp.cpp:389
KateApp::KateApp
KateApp(TDECmdLineArgs *args)
constructors & accessor to app object + plugin interface for it
Definition: kateapp.cpp:53
KateApp::mainWindows
uint mainWindows() const
give back number of existing main windows
Definition: kateapp.cpp:477
KateApp::setCursor
bool setCursor(int line, int column)
position cursor in current active view
Definition: kateapp.cpp:418
KateApp::openInput
bool openInput(const TQString &text)
helper to handle stdin input open a new document/view, fill it with the text given
Definition: kateapp.cpp:430
KateApp::sessionManager
KateSessionManager * sessionManager()
accessor to session manager
Definition: kateapp.cpp:384
KateApp::removeMainWindow
void removeMainWindow(KateMainWindow *mainWindow)
removes the mainwindow given, DOES NOT DELETE IT
Definition: kateapp.cpp:459
KateSessionManager
The Kate session manager.
Definition: katesession.h:177
KateSessionManager::saveActiveSession
void saveActiveSession()
Definition: katesession.h:339
KateSessionManager::getSessionIdFromName
int getSessionIdFromName(const TQString &name)
Return the session id of the first session whose name matches the provided one.
Definition: katesession.cpp:568
KateSessionManager::self
static KateSessionManager * self()
get a pointer to the unique KateSessionManager instance.
Definition: katesession.cpp:321
KateSessionManager::newSession
int newSession(const TQString &sessionName=TQString::null, bool saveCurr=true)
Definition: katesession.cpp:636
KateSessionManager::restoreLastSession
bool restoreLastSession()
Restore the last saved session.
Definition: katesession.cpp:673
KateSessionManager::getSwitchOption
const int getSwitchOption()
Definition: katesession.cpp:531
KateSessionManager::saveConfig
void saveConfig(bool saveSessions)
Save session manager info.
Definition: katesession.cpp:481
KateSessionManager::getStartupOption
const int getStartupOption()
Definition: katesession.cpp:524
KateSessionManager::activateSession
bool activateSession(int sessionId, bool saveCurr=true)
Activate the selected session.
Definition: katesession.cpp:583
KateSessionManager::setSwitchOption
void setSwitchOption(int option)
Set the new session switch preference.
Definition: katesession.cpp:538
Kate::Application
Interface to the application, beside some global methodes to access other objects like document/proje...
Definition: application.h:39

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kate

Skip menu "kate"
  • kate
  • libkonq
  • twin
  •   lib
Generated for kate by doxygen 1.9.1
This website is maintained by Timothy Pearson.