kateapp.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kateapp.h"
00021 #include "kateapp.moc"
00022 
00023 #include "katedocmanager.h"
00024 #include "katepluginmanager.h"
00025 #include "kateviewmanager.h"
00026 #include "kateappIface.h"
00027 #include "katesession.h"
00028 #include "katemainwindow.h"
00029 
00030 #include "../interfaces/application.h"
00031 
00032 #include <kdeversion.h>
00033 #include <kcmdlineargs.h>
00034 #include <dcopclient.h>
00035 #include <kconfig.h>
00036 #include <kwin.h>
00037 #include <ktip.h>
00038 #include <kdebug.h>
00039 #include <klibloader.h>
00040 #include <kmessagebox.h>
00041 #include <klocale.h>
00042 #include <ksimpleconfig.h>
00043 #include <kstartupinfo.h>
00044 
00045 #include <qfile.h>
00046 #include <qtimer.h>
00047 #include <qdir.h>
00048 #include <qtextcodec.h>
00049 
00050 KateApp::KateApp (KCmdLineArgs *args)
00051  : KApplication ()
00052  , m_args (args)
00053  , m_shouldExit (false)
00054 {
00055   // Don't handle DCOP requests yet
00056   dcopClient()->suspend();
00057 
00058   // insert right translations for the katepart
00059   KGlobal::locale()->insertCatalogue("katepart");
00060 
00061   // some global default
00062   Kate::Document::setFileChangedDialogsActivated (true);
00063 
00064   // application interface
00065   m_application = new Kate::Application (this);
00066 
00067   // doc + project man
00068   m_docManager = new KateDocManager (this);
00069 
00070   // init all normal plugins
00071   m_pluginManager = new KatePluginManager (this);
00072 
00073   // session manager up
00074   m_sessionManager = new KateSessionManager (this);
00075 
00076   // application dcop interface
00077   m_obj = new KateAppDCOPIface (this);
00078 
00079   // handle restore different
00080   if (isRestored())
00081   {
00082     restoreKate ();
00083   }
00084   else
00085   {
00086     // let us handle our command line args and co ;)
00087     // we can exit here if session chooser decides
00088     if (!startupKate ())
00089     {
00090       m_shouldExit = true;
00091       return;
00092     }
00093   }
00094 
00095   // Ok. We are ready for DCOP requests.
00096   dcopClient()->resume();
00097 }
00098 
00099 KateApp::~KateApp ()
00100 {
00101   // cu dcop interface
00102   delete m_obj;
00103 
00104   // cu plugin manager
00105   delete m_pluginManager;
00106 
00107   // delete this now, or we crash
00108   delete m_docManager;
00109 }
00110 
00111 KateApp *KateApp::self ()
00112 {
00113   return (KateApp *) kapp;
00114 }
00115 
00116 Kate::Application *KateApp::application ()
00117 {
00118   return m_application;
00119 }
00120 
00125 QString KateApp::kateVersion (bool fullVersion)
00126 {
00127   return fullVersion ? QString ("%1.%2.%3").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor()).arg(KDE::versionRelease())
00128            : QString ("%1.%2").arg(KDE::versionMajor() - 1).arg(KDE::versionMinor());
00129 }
00130 
00131 void KateApp::restoreKate ()
00132 {
00133   // restore the nice files ;) we need it
00134   Kate::Document::setOpenErrorDialogsActivated (false);
00135 
00136   // activate again correct session!!!
00137   sessionConfig()->setGroup("General");
00138   QString lastSession (sessionConfig()->readEntry ("Last Session", "default.katesession"));
00139   sessionManager()->activateSession (new KateSession (sessionManager(), lastSession, ""), false, false, false);
00140 
00141   m_docManager->restoreDocumentList (sessionConfig());
00142 
00143   Kate::Document::setOpenErrorDialogsActivated (true);
00144 
00145   // restore all windows ;)
00146   for (int n=1; KMainWindow::canBeRestored(n); n++)
00147     newMainWindow(sessionConfig(), QString ("%1").arg(n));
00148 
00149   // oh, no mainwindow, create one, should not happen, but make sure ;)
00150   if (mainWindows() == 0)
00151     newMainWindow ();
00152 
00153   // notify about start
00154   KStartupInfo::setNewStartupId( activeMainWindow(), startupId());
00155 }
00156 
00157 bool KateApp::startupKate ()
00158 {
00159   // user specified session to open
00160   if (m_args->isSet ("start"))
00161   {
00162     sessionManager()->activateSession (sessionManager()->giveSession (m_args->getOption("start")), false, false);
00163   }
00164   else if (!m_args->isSet( "stdin" ) && (m_args->count() == 0)) // only start session if no files specified
00165   {
00166     // let the user choose session if possible
00167     if (!sessionManager()->chooseSession ())
00168     {
00169       // we will exit kate now, notify the rest of the world we are done
00170       KStartupInfo::appStarted (startupId());
00171       return false;
00172     }
00173   }
00174 
00175   // oh, no mainwindow, create one, should not happen, but make sure ;)
00176   if (mainWindows() == 0)
00177     newMainWindow ();
00178 
00179   // notify about start
00180   KStartupInfo::setNewStartupId( activeMainWindow(), startupId());
00181 
00182   QTextCodec *codec = m_args->isSet("encoding") ? QTextCodec::codecForName(m_args->getOption("encoding")) : 0;
00183 
00184   Kate::Document::setOpenErrorDialogsActivated (false);
00185   uint id = 0;
00186   for (int z=0; z<m_args->count(); z++)
00187   {
00188     // this file is no local dir, open it, else warn
00189     bool noDir = !m_args->url(z).isLocalFile() || !QDir (m_args->url(z).path()).exists();
00190 
00191     if (noDir)
00192     {
00193       // open a normal file
00194       if (codec)
00195         id = activeMainWindow()->viewManager()->openURL( m_args->url(z), codec->name(), false );
00196       else
00197         id = activeMainWindow()->viewManager()->openURL( m_args->url(z), QString::null, false );
00198     }
00199     else
00200       KMessageBox::sorry( activeMainWindow(),
00201                           i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(m_args->url(z).url()) );
00202   }
00203 
00204   Kate::Document::setOpenErrorDialogsActivated (true);
00205 
00206   // handle stdin input
00207   if( m_args->isSet( "stdin" ) )
00208   {
00209     QTextIStream input(stdin);
00210 
00211     // set chosen codec
00212     if (codec)
00213       input.setCodec (codec);
00214 
00215     QString line;
00216     QString text;
00217 
00218     do
00219     {
00220       line = input.readLine();
00221       text.append( line + "\n" );
00222     } while( !line.isNull() );
00223 
00224     openInput (text);
00225   }
00226   else if ( id )
00227     activeMainWindow()->viewManager()->activateView( id );
00228 
00229   if ( activeMainWindow()->viewManager()->viewCount () == 0 )
00230     activeMainWindow()->viewManager()->activateView(m_docManager->firstDocument()->documentNumber());
00231 
00232   int line = 0;
00233   int column = 0;
00234   bool nav = false;
00235 
00236   if (m_args->isSet ("line"))
00237   {
00238     line = m_args->getOption ("line").toInt();
00239     nav = true;
00240   }
00241 
00242   if (m_args->isSet ("column"))
00243   {
00244     column = m_args->getOption ("column").toInt();
00245     nav = true;
00246   }
00247 
00248   if (nav)
00249     activeMainWindow()->viewManager()->activeView ()->setCursorPosition (line, column);
00250 
00251   // show the nice tips
00252   KTipDialog::showTip(activeMainWindow());
00253 
00254   return true;
00255 }
00256 
00257 void KateApp::shutdownKate (KateMainWindow *win)
00258 {
00259   if (!win->queryClose_internal())
00260     return;
00261 
00262   sessionManager()->saveActiveSession(true, true);
00263 
00264   // detach the dcopClient
00265   dcopClient()->detach();
00266 
00267   // cu main windows
00268   while (!m_mainWindows.isEmpty())
00269     delete m_mainWindows[0];
00270 
00271   quit ();
00272 }
00273 
00274 KatePluginManager *KateApp::pluginManager()
00275 {
00276   return m_pluginManager;
00277 }
00278 
00279 KateDocManager *KateApp::documentManager ()
00280 {
00281   return m_docManager;
00282 }
00283 
00284 KateSessionManager *KateApp::sessionManager ()
00285 {
00286   return m_sessionManager;
00287 }
00288 
00289 bool KateApp::openURL (const KURL &url, const QString &encoding)
00290 {
00291   KateMainWindow *mainWindow = activeMainWindow ();
00292 
00293   if (!mainWindow)
00294     return false;
00295 
00296   QTextCodec *codec = encoding.isEmpty() ? 0 : QTextCodec::codecForName(encoding.latin1());
00297 
00298   kdDebug () << "OPEN URL "<< encoding << endl;
00299 
00300   // this file is no local dir, open it, else warn
00301   bool noDir = !url.isLocalFile() || !QDir (url.path()).exists();
00302 
00303   if (noDir)
00304   {
00305     // open a normal file
00306     if (codec)
00307       mainWindow->viewManager()->openURL( url, codec->name());
00308     else
00309       mainWindow->viewManager()->openURL( url, QString::null );
00310   }
00311   else
00312     KMessageBox::sorry( mainWindow,
00313                         i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(url.url()) );
00314 
00315   return true;
00316 }
00317 
00318 bool KateApp::setCursor (int line, int column)
00319 {
00320   KateMainWindow *mainWindow = activeMainWindow ();
00321 
00322   if (!mainWindow)
00323     return false;
00324 
00325   mainWindow->viewManager()->activeView ()->setCursorPosition (line, column);
00326 
00327   return true;
00328 }
00329 
00330 bool KateApp::openInput (const QString &text)
00331 {
00332   activeMainWindow()->viewManager()->openURL( "", "", true );
00333 
00334   if (!activeMainWindow()->viewManager()->activeView ())
00335     return false;
00336 
00337   activeMainWindow()->viewManager()->activeView ()->getDoc()->setText (text);
00338 
00339   return true;
00340 }
00341 
00342 KateMainWindow *KateApp::newMainWindow (KConfig *sconfig, const QString &sgroup)
00343 {
00344   KateMainWindow *mainWindow = new KateMainWindow (sconfig, sgroup);
00345   m_mainWindows.push_back (mainWindow);
00346 
00347   if ((mainWindows() > 1) && m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView())
00348     mainWindow->viewManager()->activateView ( m_mainWindows[m_mainWindows.count()-2]->viewManager()->activeView()->getDoc()->documentNumber() );
00349   else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
00350     mainWindow->viewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
00351   else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
00352     mainWindow->viewManager()->openURL ( KURL() );
00353 
00354   mainWindow->show ();
00355 
00356   return mainWindow;
00357 }
00358 
00359 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
00360 {
00361   m_mainWindows.remove (mainWindow);
00362 }
00363 
00364 KateMainWindow *KateApp::activeMainWindow ()
00365 {
00366   if (m_mainWindows.isEmpty())
00367     return 0;
00368 
00369   int n = m_mainWindows.findIndex ((KateMainWindow *)activeWindow());
00370 
00371   if (n < 0)
00372     n=0;
00373 
00374   return m_mainWindows[n];
00375 }
00376 
00377 uint KateApp::mainWindows () const
00378 {
00379   return m_mainWindows.size();
00380 }
00381 
00382 KateMainWindow *KateApp::mainWindow (uint n)
00383 {
00384   if (n < m_mainWindows.size())
00385     return m_mainWindows[n];
00386 
00387   return 0;
00388 }
00389 
00390 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Home | KDE Accessibility Home | Description of Access Keys