• 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 <kdeversion.h>
33 #include <kcmdlineargs.h>
34 #include <dcopclient.h>
35 #include <kconfig.h>
36 #include <kwin.h>
37 #include <ktip.h>
38 #include <kdebug.h>
39 #include <klibloader.h>
40 #include <kmessagebox.h>
41 #include <klocale.h>
42 #include <ksimpleconfig.h>
43 #include <kstartupinfo.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 (KCmdLineArgs *args)
55  : KApplication ()
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  KGlobal::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 = new KateSessionManager (TQT_TQOBJECT(this));
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  // cu dcop interface
109  delete m_obj;
110 
111  // cu plugin manager
112  delete m_pluginManager;
113 
114  // delete this now, or we crash
115  delete m_docManager;
116 }
117 
118 KateApp *KateApp::self ()
119 {
120  return (KateApp *) kapp;
121 }
122 
123 Kate::Application *KateApp::application ()
124 {
125  return m_application;
126 }
127 
132 TQString KateApp::kateVersion (bool fullVersion)
133 {
134  return fullVersion ? TQString ("%1.%2.%3").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()).arg(KDE::versionRelease())
135  : TQString ("%1.%2").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor());
136 }
137 
138 void KateApp::restoreKate ()
139 {
140  // restore the nice files ;) we need it
141  Kate::Document::setOpenErrorDialogsActivated (false);
142 
143  // activate again correct session!!!
144  sessionConfig()->setGroup("General");
145  TQString lastSession (sessionConfig()->readEntry ("Last Session", "default.katesession"));
146  sessionManager()->activateSession (new KateSession (sessionManager(), lastSession, ""), false, false, false);
147 
148  m_docManager->restoreDocumentList (sessionConfig());
149 
150  Kate::Document::setOpenErrorDialogsActivated (true);
151 
152  // restore all windows ;)
153  for (int n=1; KMainWindow::canBeRestored(n); n++)
154  newMainWindow(sessionConfig(), TQString ("%1").arg(n));
155 
156  // oh, no mainwindow, create one, should not happen, but make sure ;)
157  if (mainWindows() == 0)
158  newMainWindow ();
159 
160  // Do not notify about start there: this makes kicker crazy and kate go to a wrong desktop.
161  // KStartupInfo::setNewStartupId( activeMainWindow(), startupId());
162 }
163 
164 bool KateApp::startupKate ()
165 {
166  // user specified session to open
167  if (m_args->isSet ("start"))
168  {
169  sessionManager()->activateSession (sessionManager()->giveSession (TQString::fromLocal8Bit(m_args->getOption("start"))), false, false);
170  }
171  else
172  {
173  // let the user choose session if possible
174  if (!sessionManager()->chooseSession ())
175  {
176  // we will exit kate now, notify the rest of the world we are done
177  KStartupInfo::appStarted (startupId());
178  return false;
179  }
180  }
181 
182  // oh, no mainwindow, create one, should not happen, but make sure ;)
183  if (mainWindows() == 0)
184  newMainWindow ();
185 
186  // notify about start
187  KStartupInfo::setNewStartupId( activeMainWindow(), startupId());
188 
189  TQTextCodec *codec = m_args->isSet("encoding") ? TQTextCodec::codecForName(m_args->getOption("encoding")) : 0;
190 
191  bool tempfileSet = KCmdLineArgs::isTempFileSet();
192 
193  Kate::Document::setOpenErrorDialogsActivated (false);
194  uint id = 0;
195  for (int z=0; z<m_args->count(); z++)
196  {
197  // this file is no local dir, open it, else warn
198  bool noDir = !m_args->url(z).isLocalFile() || !TQDir (m_args->url(z).path()).exists();
199 
200  if (noDir)
201  {
202  // open a normal file
203  if (codec)
204  id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false, tempfileSet );
205  else
206  id = activeMainWindow()->viewManager()->openURL( m_args->url(z), TQString::null, false, tempfileSet );
207  }
208  else
209  KMessageBox::sorry( activeMainWindow(),
210  i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).url()) );
211  }
212 
213  Kate::Document::setOpenErrorDialogsActivated (true);
214 
215  // handle stdin input
216  if( m_args->isSet( "stdin" ) )
217  {
218  TQTextIStream input(stdin);
219 
220  // set chosen codec
221  if (codec)
222  input.setCodec (codec);
223 
224  TQString line;
225  TQString text;
226 
227  do
228  {
229  line = input.readLine();
230  text.append( line + "\n" );
231  } while( !line.isNull() );
232 
233  openInput (text);
234  }
235  else if ( id )
236  activeMainWindow()->viewManager()->activateView( id );
237 
238  if ( activeMainWindow()->viewManager()->viewCount () == 0 )
239  activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber());
240 
241  int line = 0;
242  int column = 0;
243  bool nav = false;
244 
245  if (m_args->isSet ("line"))
246  {
247  line = m_args->getOption ("line").toInt();
248  nav = true;
249  }
250 
251  if (m_args->isSet ("column"))
252  {
253  column = m_args->getOption ("column").toInt();
254  nav = true;
255  }
256 
257  if (nav)
258  activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column);
259 
260  // show the nice tips
261  KTipDialog::showTip(activeMainWindow());
262 
263  return true;
264 }
265 
266 void KateApp::shutdownKate (KateMainWindow *win)
267 {
268  if (!win->queryClose_internal())
269  return;
270 
271  sessionManager()->saveActiveSession(true, true);
272 
273  // detach the dcopClient
274  dcopClient()->detach();
275 
276  // cu main windows
277  while (!m_mainWindows.isEmpty())
278  delete m_mainWindows[0];
279 
280  quit ();
281 }
282 
283 KatePluginManager *KateApp::pluginManager()
284 {
285  return m_pluginManager;
286 }
287 
288 KateDocManager *KateApp::documentManager ()
289 {
290  return m_docManager;
291 }
292 
293 KateSessionManager *KateApp::sessionManager ()
294 {
295  return m_sessionManager;
296 }
297 
298 bool KateApp::openURL (const KURL &url, const TQString &encoding, bool isTempFile)
299 {
300  KateMainWindow *mainWindow = activeMainWindow ();
301 
302  if (!mainWindow)
303  return false;
304 
305  TQTextCodec *codec = encoding.isEmpty() ? 0 : TQTextCodec::codecForName(encoding.latin1());
306 
307  kdDebug () << "OPEN URL "<< encoding << endl;
308 
309  // this file is no local dir, open it, else warn
310  bool noDir = !url.isLocalFile() || !TQDir (url.path()).exists();
311 
312  if (noDir)
313  {
314  // open a normal file
315  if (codec)
316  mainWindow->viewManager()->openURL( url, codec->name(), true, isTempFile );
317  else
318  mainWindow->viewManager()->openURL( url, TQString::null, true, isTempFile );
319  }
320  else
321  KMessageBox::sorry( mainWindow,
322  i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.url()) );
323 
324  return true;
325 }
326 
327 bool KateApp::setCursor (int line, int column)
328 {
329  KateMainWindow *mainWindow = activeMainWindow ();
330 
331  if (!mainWindow)
332  return false;
333 
334  mainWindow->viewManager()->activeView ()->setCursorPosition (line, column);
335 
336  return true;
337 }
338 
339 bool KateApp::openInput (const TQString &text)
340 {
341  activeMainWindow()->viewManager()->openURL( "", "", true );
342 
343  if (!activeMainWindow()->viewManager()->activeView ())
344  return false;
345 
346  activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text);
347 
348  return true;
349 }
350 
351 KateMainWindow *KateApp::newMainWindow (KConfig *sconfig, const TQString &sgroup)
352 {
353  KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup);
354  m_mainWindows.push_back (mainWindow);
355 
356  if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView())
357  mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() );
358  else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
359  mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
360  else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
361  mainWindow->viewManager()->openURL ( KURL() );
362 
363  mainWindow->show ();
364 
365  return mainWindow;
366 }
367 
368 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
369 {
370  m_mainWindows.remove (mainWindow);
371 }
372 
373 KateMainWindow *KateApp::activeMainWindow ()
374 {
375  if (m_mainWindows.isEmpty())
376  return 0;
377 
378  int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow());
379 
380  if (n < 0)
381  n=0;
382 
383  return m_mainWindows[n];
384 }
385 
386 uint KateApp::mainWindows () const
387 {
388  return m_mainWindows.size();
389 }
390 
391 KateMainWindow *KateApp::mainWindow (uint n)
392 {
393  if (n < m_mainWindows.size())
394  return m_mainWindows[n];
395 
396  return 0;
397 }
398 
399 // kate: space-indent on; indent-width 2; replace-tabs on;

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
  • kwin
  •   lib
  • libkonq
Generated for kate by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |