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

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.8.16
This website is maintained by Timothy Pearson.