kate Library API Documentation

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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, 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 
00028 #include <kcmdlineargs.h>
00029 #include <dcopclient.h>
00030 #include <kconfig.h>
00031 #include <kwin.h>
00032 #include <ktip.h>
00033 #include <kdebug.h>
00034 #include <klibloader.h>
00035 #include <kmessagebox.h>
00036 #include <klocale.h>
00037 #include <ksimpleconfig.h>
00038 #include <kstartupinfo.h>
00039 
00040 #include <qfile.h>
00041 #include <qtimer.h>
00042 #include <qdir.h>
00043 #include <qtextcodec.h>
00044 
00045 KConfig *KateApp::m_sessionConfig = 0;
00046 
00047 KateApp::KateApp (bool forcedNewProcess, bool oldState)
00048  : KUniqueApplication (true,true,true)
00049  , m_firstStart (true)
00050  , m_initPlugin (0)
00051  , m_doNotInitialize (0)
00052 {
00053   // we need to call that now, don't ask me, in the first newInstance run it is wrong !
00054   if (isRestored())
00055   {
00056     m_sessionConfig = sessionConfig ();
00057     m_sessionConfigDelete = false;
00058   }
00059   else // no restoring, use our own katesessionrc from start on !
00060   {
00061     m_sessionConfig = new KSimpleConfig ("katesessionrc", false);
00062     m_sessionConfigDelete = true;
00063   }
00064 
00065   // Don't handle DCOP requests yet
00066   kapp->dcopClient()->suspend();
00067 
00068   m_mainWindows.setAutoDelete (false);
00069 
00070   // uh, we have forced this session to come up via changing config
00071   // change it back now
00072   if (forcedNewProcess)
00073   {
00074     config()->setGroup("KDE");
00075     config()->writeEntry("MultipleInstances",oldState);
00076     config()->sync();
00077   }
00078 
00079   // application interface
00080   m_application = new Kate::Application (this);
00081 
00082   // init plugin manager
00083   m_initPluginManager = new Kate::InitPluginManager (this);
00084 
00085   // application dcop interface
00086   m_obj = new KateAppDCOPIface (this);
00087 
00088   // insert right translations for the katepart
00089   KGlobal::locale()->insertCatalogue("katepart");
00090 
00091   // doc + project man
00092   m_docManager = new KateDocManager (this);
00093   m_projectManager = new KateProjectManager (this);
00094 
00095   // init all normal plugins
00096   m_pluginManager = new KatePluginManager (this);
00097 
00098   KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00099 
00100   if (args->isSet("initplugin"))
00101   {
00102     QString pluginName=args->getOption("initplugin");
00103 
00104     m_initURL=args->url(0);
00105 
00106     m_initPlugin= static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(pluginName), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00107 
00108     m_initPlugin->activate(args->url(0));
00109 
00110     m_doNotInitialize=m_initPlugin->actionsKateShouldNotPerformOnRealStartup();
00111 
00112 //      kdDebug(13001)<<"********************loading init plugin in app constructor"<<endl;
00113   }
00114 
00115   // Ok. We are ready for DCOP requests.
00116   kapp->dcopClient()->resume();
00117 
00118   // notify our self on enter the event loop
00119   QTimer::singleShot(10,this,SLOT(callOnEventLoopEnter()));
00120 }
00121 
00122 KateApp::~KateApp ()
00123 {
00124   // cu dcop interface
00125   delete m_obj;
00126 
00127   // cu plugin manager
00128   delete m_pluginManager;
00129 
00130   // cu project man
00131   delete m_projectManager;
00132 
00133   // delete this now, or we crash
00134   delete m_docManager;
00135 
00136   // our session config is our own one, cleanup
00137   if (m_sessionConfigDelete)
00138     delete m_sessionConfig;
00139 }
00140 
00141 void KateApp::callOnEventLoopEnter()
00142 {
00143   emit onEventLoopEnter();
00144   disconnect(this,SIGNAL(onEventLoopEnter()),0,0);
00145   emit m_application->onEventLoopEnter();
00146   disconnect(m_application,SIGNAL(onEventLoopEnter()),0,0);
00147 
00148 //   kdDebug(13001)<<"callOnEventLoopEnter(): "<<kapp->loopLevel()<<"*****************************"<<endl;
00149 }
00150 
00151 void KateApp::performInit(const QString &libname, const KURL &url)
00152 {
00153   if (m_initPlugin)
00154     m_oldInitLib=m_initLib;
00155   else
00156     m_oldInitLib=QString::null;
00157 
00158   m_initURL=url;
00159   m_initLib=libname;
00160 
00161   QTimer::singleShot(0,this,SLOT(performInit()));
00162 }
00163 
00164 void KateApp::performInit()
00165 {
00166   if (( m_oldInitLib.isNull()) || (m_oldInitLib!=m_initLib))
00167   {
00168     delete m_initPlugin;
00169     m_initPlugin=0;
00170 
00171     if (!m_oldInitLib.isNull())
00172       KLibLoader::self()->unloadLibrary(m_oldInitLib.latin1());
00173 
00174     m_initPlugin = static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(m_initLib), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00175   }
00176 
00177   m_initPlugin->activate(m_initURL);
00178   m_initPlugin->initKate();
00179 }
00180 
00181 Kate::InitPlugin *KateApp::initPlugin() const
00182 {
00183   return m_initPlugin;
00184 }
00185 
00186 KURL KateApp::initScript() const {return m_initURL;}
00187 
00188 int KateApp::newInstance()
00189 {
00190   if (m_firstStart)
00191   {
00192     // we restore our great stuff here now ;) super
00193     if ( restoringSession() )
00194     {
00195       // restore the nice projects & files ;) we need it
00196       Kate::Document::setOpenErrorDialogsActivated (false);
00197 
00198       m_projectManager->restoreProjectList (sessionConfig());
00199       m_docManager->restoreDocumentList (sessionConfig());
00200 
00201       Kate::Document::setOpenErrorDialogsActivated (true);
00202 
00203       for (int n=1; KMainWindow::canBeRestored(n); n++)
00204       {
00205         KateMainWindow *win=newMainWindow(false);
00206         win->restore ( n, true );
00207       }
00208     }
00209     else
00210     {
00211       Kate::Document::setOpenErrorDialogsActivated (false);
00212       config()->setGroup("General");
00213 
00214       // restore our nice projects if wanted
00215       if (config()->readBoolEntry("Restore Projects", false))
00216         m_projectManager->restoreProjectList (kateSessionConfig ());
00217 
00218       // reopen our nice files if wanted
00219       if (config()->readBoolEntry("Restore Documents", false))
00220         m_docManager->restoreDocumentList (kateSessionConfig ());
00221 
00222       KateMainWindow *win=newMainWindow(false);
00223 
00224       // window config
00225       config()->setGroup("General");
00226       if (config()->readBoolEntry("Restore Window Configuration", false))
00227         win->readProperties (kateSessionConfig ());
00228 
00229       Kate::Document::setOpenErrorDialogsActivated (true);
00230       win->show ();
00231     }
00232   }
00233 
00234   // oh, no mainwindow, create one, should not happen, but make sure ;)
00235   if (mainWindows() == 0)
00236     newMainWindow ();
00237 
00238   KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00239 
00240   // search the right main window
00241   // or create one if no window on the current desktop
00242   KateMainWindow *win = 0;
00243 
00244   if (!m_firstStart && args->isSet ("w"))
00245   {
00246     win = newMainWindow ();
00247   }
00248   else
00249   {
00250     if (activeKateMainWindow() && KWin::windowInfo (activeKateMainWindow()->winId()).isOnCurrentDesktop())
00251     {
00252       win = activeKateMainWindow();
00253     }
00254     else
00255     {
00256       for (uint i=0; i < m_mainWindows.count(); i++)
00257       {
00258         if (KWin::windowInfo (m_mainWindows.at(i)->winId()).isOnCurrentDesktop())
00259         {
00260           win = m_mainWindows.at(i);
00261           break;
00262         }
00263       }
00264     }
00265 
00266     // create window on current desktop
00267     if (!win)
00268       win = newMainWindow ();
00269   }
00270 
00271   // raise the window if not first start
00272   if (!m_firstStart)
00273   {
00274     win->raise();
00275     KWin::activateWindow (win->winId());
00276   }
00277 
00278   if (m_firstStart && m_initPlugin)
00279   {
00280     m_initPlugin->initKate();
00281   }
00282   else if (args->isSet("initplugin"))
00283   {
00284     performInit(args->getOption("initplugin"),args->url(0));
00285   }
00286   else
00287   {
00288     QTextCodec *codec = args->isSet("encoding") ? QTextCodec::codecForName(args->getOption("encoding")) : 0;
00289 
00290     Kate::Document::setOpenErrorDialogsActivated (false);
00291     uint id = 0;
00292     for (int z=0; z<args->count(); z++)
00293     {
00294       // this file is no local dir, open it, else warn
00295       bool noDir = !args->url(z).isLocalFile() || !QDir (args->url(z).path()).exists();
00296 
00297       if (noDir)
00298       {
00299         if (args->url(z).isLocalFile () && args->url(z).path().endsWith(".kateproject")) // open a project file
00300           m_mainWindows.first()->openProject ( args->url(z).path() );
00301         else
00302         {
00303           // open a normal file
00304 
00305           if (codec)
00306             id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), codec->name(), false );
00307           else
00308             id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), QString::null, false );
00309         }
00310       }
00311       else
00312         KMessageBox::sorry( m_mainWindows.first(),
00313                             i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(args->url(z).url()) );
00314     }
00315 
00316     if ( id )
00317       m_mainWindows.first()->kateViewManager()->activateView( id );
00318 
00319     Kate::Document::setOpenErrorDialogsActivated (true);
00320 
00321     if ( m_mainWindows.first()->kateViewManager()->viewCount () == 0 )
00322       m_mainWindows.first()->kateViewManager()->activateView(m_docManager->firstDocument()->documentNumber());
00323 
00324     int line = 0;
00325     int column = 0;
00326     bool nav = false;
00327 
00328     if (args->isSet ("line"))
00329     {
00330       line = args->getOption ("line").toInt();
00331       nav = true;
00332     }
00333 
00334     if (args->isSet ("column"))
00335     {
00336       column = args->getOption ("column").toInt();
00337       nav = true;
00338     }
00339 
00340     if (nav)
00341       m_mainWindows.first()->kateViewManager()->activeView ()->setCursorPosition (line, column);
00342   }
00343 
00344   KStartupInfo::setNewStartupId( m_mainWindows.first(), kapp->startupId());
00345 
00346   if (m_firstStart)
00347   {
00348     // very important :)
00349     m_firstStart = false;
00350 
00351     // show the nice tips
00352     KTipDialog::showTip(m_mainWindows.first());
00353   }
00354 
00355   return 0;
00356 }
00357 
00358 KateMainWindow *KateApp::newMainWindow ()
00359 {
00360   return newMainWindow (true);
00361 }
00362 
00363 KateMainWindow *KateApp::newMainWindow (bool visible)
00364 {
00365   KateMainWindow *mainWindow = new KateMainWindow ();
00366   m_mainWindows.append (mainWindow);
00367 
00368   if ((mainWindows() > 1) && m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView())
00369     mainWindow->kateViewManager()->activateView ( m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView()->getDoc()->documentNumber() );
00370   else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
00371     mainWindow->kateViewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
00372   else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
00373     mainWindow->kateViewManager()->openURL ( KURL() );
00374 
00375   if (visible)
00376     mainWindow->show ();
00377 
00378 
00379   if (!m_firstStart)
00380   {
00381     mainWindow->raise();
00382     KWin::activateWindow (mainWindow->winId());
00383   }
00384 
00385   return mainWindow;
00386 }
00387 
00388 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
00389 {
00390   m_mainWindows.remove (mainWindow);
00391 }
00392 
00393 void KateApp::openURL (const QString &name)
00394 {
00395   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00396 
00397   if (n < 0)
00398     n=0;
00399 
00400   m_mainWindows.at(n)->kateViewManager()->openURL (KURL(name));
00401 
00402   if ( ! m_firstStart )
00403   {
00404     m_mainWindows.at(n)->raise();
00405     KWin::activateWindow (m_mainWindows.at(n)->winId());
00406   }
00407 }
00408 
00409 KateMainWindow *KateApp::activeKateMainWindow ()
00410 {
00411   if (m_mainWindows.isEmpty())
00412     return 0;
00413 
00414   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00415 
00416   if (n < 0)
00417     n=0;
00418 
00419   return m_mainWindows.at(n);
00420 }
00421 
00422 Kate::MainWindow *KateApp::activeMainWindow ()
00423 {
00424   if (!activeKateMainWindow())
00425     return 0;
00426 
00427   return activeKateMainWindow()->mainWindow();
00428 }
00429 
00430 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 6 02:40:57 2005 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003