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