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 
00039 #include <kio/netaccess.h>
00040 
00041 #include <qfile.h>
00042 #include <qtimer.h>
00043 #include <kmdidefines.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  , m_restoreGUIMode (KMdi::UndefinedMode)
00053 {
00054   // we need to call that now, don't ask me, in the first newInstance run it is wrong !
00055   if (isRestored())
00056   {
00057     m_sessionConfig = sessionConfig ();
00058     m_sessionConfigDelete = false;
00059   }
00060   else // no restoring, use our own katesessionrc from start on !
00061   {
00062     m_sessionConfig = new KSimpleConfig ("katesessionrc", false);
00063     m_sessionConfigDelete = true;
00064   }
00065 
00066   // Don't handle DCOP requests yet
00067   kapp->dcopClient()->suspend();
00068 
00069   m_mainWindows.setAutoDelete (false);
00070 
00071   // uh, we have forced this session to come up via changing config
00072   // change it back now
00073   if (forcedNewProcess)
00074   {
00075     config()->setGroup("KDE");
00076     config()->writeEntry("MultipleInstances",oldState);
00077     config()->sync();
00078   }
00079 
00080   // application interface
00081   m_application = new Kate::Application (this);
00082 
00083   // init plugin manager
00084   m_initPluginManager = new Kate::InitPluginManager (this);
00085 
00086   // application dcop interface
00087   m_obj = new KateAppDCOPIface (this);
00088 
00089   // insert right translations for the katepart
00090   KGlobal::locale()->insertCatalogue("katepart");
00091 
00092   // doc + project man
00093   m_docManager = new KateDocManager (this);
00094   m_projectManager = new KateProjectManager (this);
00095 
00096   // init all normal plugins
00097   m_pluginManager = new KatePluginManager (this);
00098 
00099   KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00100 
00101   if (args->isSet("initplugin"))
00102   {
00103     QString pluginName=args->getOption("initplugin");
00104 
00105     m_initURL=args->url(0);
00106 
00107     m_initPlugin= static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(pluginName), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00108 
00109     m_initPlugin->activate(args->url(0));
00110 
00111     m_doNotInitialize=m_initPlugin->actionsKateShouldNotPerformOnRealStartup();
00112 
00113     kdDebug(13001)<<"********************loading init plugin in app constructor"<<endl;
00114   }
00115 
00116   // Ok. We are ready for DCOP requests.
00117   kapp->dcopClient()->resume();
00118 
00119   // notify our self on enter the event loop
00120   QTimer::singleShot(10,this,SLOT(callOnEventLoopEnter()));
00121 }
00122 
00123 KateApp::~KateApp ()
00124 {
00125   // cu dcop interface
00126   delete m_obj;
00127 
00128   // cu plugin manager
00129   delete m_pluginManager;
00130 
00131   // cu project man
00132   delete m_projectManager;
00133 
00134   // delete this now, or we crash
00135   delete m_docManager;
00136 
00137   // our session config is our own one, cleanup
00138   if (m_sessionConfigDelete)
00139     delete m_sessionConfig;
00140 }
00141 
00142 void KateApp::callOnEventLoopEnter()
00143 {
00144   emit onEventLoopEnter();
00145   disconnect(this,SIGNAL(onEventLoopEnter()),0,0);
00146   emit m_application->onEventLoopEnter();
00147   disconnect(m_application,SIGNAL(onEventLoopEnter()),0,0);
00148 
00149   kdDebug(13001)<<"callOnEventLoopEnter(): "<<kapp->loopLevel()<<"*****************************"<<endl;
00150 }
00151 
00152 void KateApp::performInit(const QString &libname, const KURL &url)
00153 {
00154   if (m_initPlugin)
00155     m_oldInitLib=m_initLib;
00156   else
00157     m_oldInitLib=QString::null;
00158 
00159   m_initURL=url;
00160   m_initLib=libname;
00161 
00162   QTimer::singleShot(0,this,SLOT(performInit()));
00163 }
00164 
00165 void KateApp::performInit()
00166 {
00167   if (( m_oldInitLib.isNull()) || (m_oldInitLib!=m_initLib))
00168   {
00169     delete m_initPlugin;
00170     m_initPlugin=0;
00171 
00172     if (!m_oldInitLib.isNull())
00173       KLibLoader::self()->unloadLibrary(m_oldInitLib.latin1());
00174 
00175     m_initPlugin = static_cast<Kate::InitPlugin*>(Kate::createPlugin (QFile::encodeName(m_initLib), (Kate::Application *)kapp)->qt_cast("Kate::InitPlugin"));
00176   }
00177 
00178   m_initPlugin->activate(m_initURL);
00179   m_initPlugin->initKate();
00180 }
00181 
00182 Kate::InitPlugin *KateApp::initPlugin() const
00183 {
00184   return m_initPlugin;
00185 }
00186 
00187 KURL KateApp::initScript() const {return m_initURL;}
00188 
00189 int KateApp::newInstance()
00190 {
00191   if (m_firstStart)
00192   {
00193     // we restore our great stuff here now ;) super
00194     if ( isRestored() )
00195     {
00196       // restore the nice projects & files ;) we need it
00197       m_projectManager->restoreProjectList (sessionConfig());
00198       m_docManager->restoreDocumentList (sessionConfig());
00199 
00200       for (int n=1; KMainWindow::canBeRestored(n); n++)
00201       {
00202         KateMainWindow *win=newMainWindow(false);
00203         win->restore ( n, true );
00204       }
00205     }
00206     else
00207     {
00208       config()->setGroup("General");
00209 
00210       // restore our nice projects if wanted
00211       if (config()->readBoolEntry("Restore Projects", false))
00212         m_projectManager->restoreProjectList (kateSessionConfig ());
00213 
00214       // reopen our nice files if wanted
00215       if (config()->readBoolEntry("Restore Documents", false))
00216         m_docManager->restoreDocumentList (kateSessionConfig ());
00217 
00218       KateMainWindow *win=newMainWindow(false);
00219 
00220       // window config
00221       config()->setGroup("General");
00222       if (config()->readBoolEntry("Restore Window Configuration", false))
00223         win->readProperties (kateSessionConfig ());
00224 
00225       win->show ();
00226     }
00227   }
00228 
00229   // oh, no mainwindow, create one, should not happen, but make sure ;)
00230   if (mainWindows() == 0)
00231     newMainWindow ();
00232 
00233   KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00234 
00235   if (!m_firstStart && args->isSet ("w"))
00236     newMainWindow ();
00237 
00238   if (!m_firstStart)
00239     raiseCurrentMainWindow ();
00240 
00241   kdDebug(13001)<<"******************************************** loop depth"<<kapp->loopLevel()<<endl;
00242 
00243   if (m_firstStart && m_initPlugin)
00244   {
00245     m_initPlugin->initKate();
00246     kdDebug(13001)<<"***************************** INIT PLUGIN ON FIRST START"<<endl;
00247   }
00248   else if (args->isSet("initplugin"))
00249   {
00250     kdDebug(13001)<<"***************************** INIT PLUGIN ON ANY  START"<<endl;
00251     performInit(args->getOption("initplugin"),args->url(0));
00252   }
00253   else
00254   {
00255     Kate::Document::setOpenErrorDialogsActivated (false);
00256     for (int z=0; z<args->count(); z++)
00257     {
00258       if (!KIO::NetAccess::mimetype( args->url(z), m_mainWindows.first() ).startsWith(QString ("inode/directory")))
00259         m_mainWindows.first()->kateViewManager()->openURL( args->url(z) );
00260       else
00261         KMessageBox::sorry( m_mainWindows.first(), i18n("The file '%1' could not be opened, it is not a normal file, it is a folder!").arg(args->url(z).url()) );
00262     }
00263     Kate::Document::setOpenErrorDialogsActivated (true);
00264 
00265     if ( m_mainWindows.first()->kateViewManager()->viewCount () == 0 )
00266       m_mainWindows.first()->kateViewManager()->activateView(m_docManager->firstDocument()->documentNumber());
00267 
00268     int line = 0;
00269     int column = 0;
00270     bool nav = false;
00271 
00272     if (args->isSet ("line"))
00273     {
00274       line = args->getOption ("line").toInt();
00275       nav = true;
00276     }
00277 
00278     if (args->isSet ("column"))
00279     {
00280       column = args->getOption ("column").toInt();
00281       nav = true;
00282     }
00283 
00284     if (nav)
00285       m_mainWindows.first()->kateViewManager()->activeView ()->setCursorPosition (line, column);
00286   }
00287 
00288   if (m_firstStart)
00289   {
00290     // very important :)
00291     m_firstStart = false;
00292 
00293     // show the nice tips
00294     KTipDialog::showTip(m_mainWindows.first());
00295   }
00296 
00297   return 0;
00298 }
00299 
00300 KateMainWindow *KateApp::newMainWindow ()
00301 {
00302   return newMainWindow (true);
00303 }
00304 
00305 KateMainWindow *KateApp::newMainWindow (bool visible)
00306 {
00307   if (KateMainWindow::defaultMode==KMdi::UndefinedMode)
00308   {
00309     KConfig *cfg=kapp->config();
00310 
00311     QString grp=cfg->group();
00312 
00313     cfg->setGroup("General");
00314     KateMainWindow::defaultMode = (KMdi::MdiMode) cfg->readNumEntry("DefaultGUIMode", KMdi::IDEAlMode);
00315 
00316     // only allow ideal or tabpage mode, others don't work right atm
00317     if ((KateMainWindow::defaultMode != KMdi::IDEAlMode) && (KateMainWindow::defaultMode != KMdi::TabPageMode))
00318       KateMainWindow::defaultMode = KMdi::IDEAlMode;
00319 
00320     cfg->setGroup(grp);
00321   }
00322   KateMainWindow *mainWindow = new KateMainWindow (m_docManager, m_pluginManager, m_projectManager,
00323                                                     (m_restoreGUIMode==KMdi::UndefinedMode)?KateMainWindow::defaultMode:m_restoreGUIMode);
00324   m_mainWindows.append (mainWindow);
00325 
00326   if ((mainWindows() > 1) && m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView())
00327     mainWindow->kateViewManager()->activateView ( m_mainWindows.at(m_mainWindows.count()-2)->kateViewManager()->activeView()->getDoc()->documentNumber() );
00328   else if ((mainWindows() > 1) && (m_docManager->documents() > 0))
00329     mainWindow->kateViewManager()->activateView ( (m_docManager->document(m_docManager->documents()-1))->documentNumber() );
00330   else if ((mainWindows() > 1) && (m_docManager->documents() < 1))
00331     mainWindow->kateViewManager()->openURL ( KURL() );
00332 
00333   if (visible)
00334     mainWindow->show ();
00335 
00336   if (!m_firstStart)
00337   {
00338     mainWindow->raise();
00339     KWin::activateWindow (mainWindow->winId());
00340   }
00341 
00342   return mainWindow;
00343 }
00344 
00345 void KateApp::removeMainWindow (KateMainWindow *mainWindow)
00346 {
00347   m_mainWindows.remove (mainWindow);
00348 }
00349 
00350 void KateApp::openURL (const QString &name)
00351 {
00352   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00353 
00354   if (n < 0)
00355     n=0;
00356 
00357   m_mainWindows.at(n)->kateViewManager()->openURL (KURL(name));
00358 
00359   m_mainWindows.at(n)->raise();
00360   KWin::activateWindow (m_mainWindows.at(n)->winId());
00361 }
00362 
00363 KateMainWindow *KateApp::activeKateMainWindow ()
00364 {
00365   if (m_mainWindows.isEmpty())
00366     return 0;
00367 
00368   int n = m_mainWindows.find ((KateMainWindow *)activeWindow());
00369 
00370   if (n < 0)
00371     n=0;
00372 
00373   return m_mainWindows.at(n);
00374 }
00375 
00376 Kate::MainWindow *KateApp::activeMainWindow ()
00377 {
00378   if (!activeKateMainWindow())
00379     return 0;
00380 
00381   return activeKateMainWindow()->mainWindow();
00382 }
00383 
00384 void KateApp::raiseCurrentMainWindow ()
00385 {
00386   if (!activeKateMainWindow())
00387     return;
00388 
00389   activeKateMainWindow()->raise();
00390   KWin::activateWindow (activeKateMainWindow()->winId());
00391 }
KDE Logo
This file is part of the documentation for kate Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 29 21:20:36 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003