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     for( uint i = 0; i < m_mainWindows.count(); i++ )
00251     {
00252       if( KWin::windowInfo( m_mainWindows.at(i)->winId()).isOnCurrentDesktop() )
00253       {
00254         win = m_mainWindows.at(i);
00255         break;
00256       }
00257     }
00258 
00259     if (!win)
00260     {
00261       if (m_mainWindows.count() > 0)
00262         // reuse existing window
00263         win = m_mainWindows.at(0);
00264       else
00265         // create window on current desktop
00266         win = newMainWindow ();
00267     }
00268   }
00269 
00270   // raise the window if not first start
00271   if (!m_firstStart)
00272   {
00273     win->raise();
00274     KWin::activateWindow (win->winId());
00275   }
00276 
00277   if (m_firstStart && m_initPlugin)
00278   {
00279     m_initPlugin->initKate();
00280   }
00281   else if (args->isSet("initplugin"))
00282   {
00283     performInit(args->getOption("initplugin"),args->url(0));
00284   }
00285   else
00286   {
00287     QTextCodec *codec = args->isSet("encoding") ? QTextCodec::codecForName(args->getOption("encoding")) : 0;
00288 
00289     Kate::Document::setOpenErrorDialogsActivated (false);
00290     uint id = 0;
00291     for (int z=0; z<args->count(); z++)
00292     {
00293       // this file is no local dir, open it, else warn
00294       bool noDir = !args->url(z).isLocalFile() || !QDir (args->url(z).path()).exists();
00295 
00296       if (noDir)
00297       {
00298         if (args->url(z).isLocalFile () && args->url(z).path().endsWith(".kateproject")) // open a project file
00299           m_mainWindows.first()->openProject ( args->url(z).path() );
00300         else
00301         {
00302           // open a normal file
00303 
00304           if (codec)
00305             id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), codec->name(), false );
00306           else
00307             id = m_mainWindows.first()->kateViewManager()->openURL( args->url(z), QString::null, false );
00308         }
00309       }
00310       else
00311         KMessageBox::sorry( m_mainWindows.first(),
00312                             i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.").arg(args->url(z).url()) );
00313     }
00314 
00315     if ( id )
00316       m_mainWindows.first()->kateViewManager()->activateView( id );
00317 
00318     Kate::Document::setOpenErrorDialogsActivated (true);
00319 
00320     if ( m_mainWindows.first()->kateViewManager()->viewCount () == 0 )
00321       m_mainWindows.first()->kateViewManager()->activateView(m_docManager->firstDocument()->documentNumber());
00322 
00323     int line = 0;
00324     int column = 0;
00325     bool nav = false;
00326 
00327     if (args->isSet ("line"))
00328     {
00329       line = args->getOption ("line").toInt();
00330       nav = true;
00331     }
00332 
00333     if (args->isSet ("column"))
00334     {
00335       column = args->getOption ("column").toInt();
00336       nav = true;
00337     }
00338 
00339     if (nav)
00340       m_mainWindows.first()->kateViewManager()->activeView ()->setCursorPosition (line, column);
00341   }
00342 
00343   KStartupInfo::setNewStartupId( win, kapp->startupId());
00344 
00345   if (m_firstStart)
00346   {
00347     // very important :)
00348     m_firstStart = false;
00349 
00350     // show the nice tips
00351     KTipDialog::showTip(m_mainWindows.first());
00352   }
00353 
00354   return 0;
00355 }
00356 
00357 KateMainWindow *KateApp::newMainWindow ()
00358 {
00359   return newMainWindow (true);
00360 }
00361 
00362 KateMainWindow *KateApp::newMainWindow (bool visible)
00363 {
00364   KateMainWindow *mainWindow = new KateMainWindow ();
00365   m_mainWindows.append (mainWindow);
00366 
00367   if ((mainWindows() > 1) && m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView())
00368     mainWindow->kateViewManager()->activateView ( m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView()->getDoc()->documentNumber() );
00369   else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
00370     mainWindow->kateViewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
00371   else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
00372     mainWindow->kateViewManager()->openURL ( KURL() );
00373 
00374   if (visible)
00375     mainWindow->show ();
00376 
00377 
00378   if (!m_firstStart)
00379   {
00380     mainWindow->raise();
00381     KWin::activateWindow (mainWindow->winId());
00382   }
00383 
00384   return mainWindow;
00385 }
00386 
00387 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
00388 {
00389   m_mainWindows.remove (mainWindow);
00390 }
00391 
00392 void KateApp::openURL (const QString &name)
00393 {
00394   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00395 
00396   if (n < 0)
00397     n=0;
00398 
00399   m_mainWindows.at(n)->kateViewManager()->openURL (KURL(name));
00400 
00401   if ( ! m_firstStart )
00402   {
00403     m_mainWindows.at(n)->raise();
00404     KWin::activateWindow (m_mainWindows.at(n)->winId());
00405   }
00406 }
00407 
00408 KateMainWindow *KateApp::activeKateMainWindow ()
00409 {
00410   if (m_mainWindows.isEmpty())
00411     return 0;
00412 
00413   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00414 
00415   if (n < 0)
00416     n=0;
00417 
00418   return m_mainWindows.at(n);
00419 }
00420 
00421 Kate::MainWindow *KateApp::activeMainWindow ()
00422 {
00423   if (!activeKateMainWindow())
00424     return 0;
00425 
00426   return activeKateMainWindow()->mainWindow();
00427 }
00428 
00429 // 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.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 13 19:27:53 2006 by doxygen 1.4.3 written by Dimitri van Heesch, © 1997-2003