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