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

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