kate Library API Documentation

katemainwindow.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00004    Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 //BEGIN Includes
00022 #include "katemainwindow.h"
00023 #include "katemainwindow.moc"
00024 
00025 #include "kateconfigdialog.h"
00026 #include "kateconsole.h"
00027 #include "katedocmanager.h"
00028 #include "katepluginmanager.h"
00029 #include "kateconfigplugindialogpage.h"
00030 #include "kateviewmanager.h"
00031 #include "kateapp.h"
00032 #include "kateprojectlist.h"
00033 #include "kateprojectviews.h"
00034 #include "katefileselector.h"
00035 #include "katefilelist.h"
00036 #include "kategrepdialog.h"
00037 #include "katemailfilesdialog.h"
00038 #include "katemainwindowiface.h"
00039 #include "kateexternaltools.h"
00040 #include "katesavemodifieddialog.h"
00041 #include "katemwmodonhddialog.h"
00042 
00043 #include <kmdi/tabwidget.h>
00044 
00045 #include <dcopclient.h>
00046 #include <kinstance.h>
00047 #include <kaboutdata.h>
00048 #include <kaction.h>
00049 #include <kapplication.h>
00050 #include <kcmdlineargs.h>
00051 #include <kdebug.h>
00052 #include <kdialogbase.h>
00053 #include <kdiroperator.h>
00054 #include <kdockwidget.h>
00055 #include <kedittoolbar.h>
00056 #include <kfiledialog.h>
00057 #include <kglobalaccel.h>
00058 #include <kglobal.h>
00059 #include <kglobalsettings.h>
00060 #include <kiconloader.h>
00061 #include <kkeydialog.h>
00062 #include <klocale.h>
00063 #include <kmessagebox.h>
00064 #include <kmimetype.h>
00065 #include <kopenwith.h>
00066 #include <kpopupmenu.h>
00067 #include <ksimpleconfig.h>
00068 #include <kstatusbar.h>
00069 #include <kstdaction.h>
00070 #include <kstandarddirs.h>
00071 #include <ktrader.h>
00072 #include <kuniqueapplication.h>
00073 #include <kurldrag.h>
00074 #include <kdesktopfile.h>
00075 #include <khelpmenu.h>
00076 #include <kmultitabbar.h>
00077 #include <ktip.h>
00078 #include <kmenubar.h>
00079 #include <kstringhandler.h>
00080 #include <qlayout.h>
00081 #include <qptrvector.h>
00082 
00083 #include <assert.h>
00084 #include <unistd.h>
00085 //END
00086 
00087 uint KateMainWindow::uniqueID = 1;
00088 
00089 KateMainWindow::KateMainWindow () :
00090     KMDI::MainWindow (0,(QString("__KateMainWindow#%1").arg(uniqueID)).latin1())
00091 {
00092   setToolViewStyle(KMultiTabBar::KDEV3ICON);
00093   // make the dockwidgets keep their size if possible
00094   manager()->setSplitterKeepSize(true);
00095   // first the very important id
00096   myID = uniqueID;
00097   uniqueID++;
00098 
00099   // init some vars
00100   m_project = 0;
00101   m_projectNumber = 0;
00102 
00103   m_modignore = false;
00104 
00105   console = 0;
00106   greptool = 0;
00107 
00108   // now the config
00109   KConfig *config = kapp->config();
00110 
00111   // first init size while we are still invisible, avoid flicker
00112   if (!initialGeometrySet())
00113   {
00114     config->setGroup ("Kate Main Window");
00115     int scnum = QApplication::desktop()->screenNumber(parentWidget());
00116     QRect desk = QApplication::desktop()->screenGeometry(scnum);
00117     QSize s ( config->readNumEntry( QString::fromLatin1("Width %1").arg(desk.width()), 700 ),
00118               config->readNumEntry( QString::fromLatin1("Height %1").arg(desk.height()), 480 ) );
00119 
00120     resize (kMin (s.width(), desk.width()), kMin(s.height(), desk.height()));
00121   }
00122 
00123   // apply settings
00124   applyMainWindowSettings(kapp->config(), "Kate Main Window");
00125 
00126   m_mainWindow = new Kate::MainWindow (this);
00127   m_toolViewManager = new Kate::ToolViewManager (this);
00128 
00129   m_dcop = new KateMainWindowDCOPIface (this);
00130 
00131   // setup the most important widgets
00132   setupMainWindow();
00133 
00134   // setup the actions
00135   setupActions();
00136 
00137   setStandardToolBarMenuEnabled( true );
00138   setXMLFile( "kateui.rc" );
00139   createShellGUI ( true );
00140 
00141   KatePluginManager::self()->enableAllPluginsGUI (this);
00142 
00143   // connect settings menu aboutToshow
00144   documentMenu = (QPopupMenu*)factory()->container("documents", this);
00145   connect(documentMenu, SIGNAL(aboutToShow()), this, SLOT(documentMenuAboutToShow()));
00146 
00147   connect(KateProjectManager::self()->projectManager(),SIGNAL(projectDeleted(uint)),this,SLOT(projectDeleted(uint)));
00148 
00149   // caption update
00150   for (uint i = 0; i < KateDocManager::self()->documents(); i++)
00151     slotDocumentCreated (KateDocManager::self()->document(i));
00152 
00153   connect(KateDocManager::self(),SIGNAL(documentCreated(Kate::Document *)),this,SLOT(slotDocumentCreated(Kate::Document *)));
00154 
00155   readOptions(config);
00156 
00157   if (console)
00158     console->loadConsoleIfNeeded();
00159 
00160   setAcceptDrops(true);
00161 
00162   // activate the first restored project, if any
00163   if (KateProjectManager::self()->projects() > 0)
00164     activateProject(KateProjectManager::self()->project(0));
00165   else
00166     activateProject(0);
00167 }
00168 
00169 KateMainWindow::~KateMainWindow()
00170 {
00171   saveOptions(kapp->config());
00172 
00173   ((KateApp *)kapp)->removeMainWindow (this);
00174 
00175   KatePluginManager::self()->disableAllPluginsGUI (this);
00176 
00177   delete m_dcop;
00178   delete kscript;
00179 }
00180 
00181 void KateMainWindow::setupMainWindow ()
00182 {
00183   greptool = new GrepTool( this, "greptool" );
00184   greptool->installEventFilter( this );
00185   connect(greptool, SIGNAL(itemSelected(const QString &,int)), this, SLOT(slotGrepToolItemSelected(const QString &,int)));
00186   // WARNING HACK - anders: showing the greptool seems to make the menu accels work
00187   greptool->show();
00188   greptool->hide();
00189 
00190   m_viewManager = new KateViewManager (this);
00191 
00192   filelist = new KateFileList (this, m_viewManager, this/*filelistDock*/, "filelist");
00193   filelist->readConfig(kapp->config(), "Filelist");
00194   addToolView(KDockWidget::DockLeft,filelist,SmallIcon("kmultiple"), i18n("Documents"));
00195 
00196   QVBox *prBox = new QVBox (this,"projects");
00197   addToolView(KDockWidget::DockLeft,prBox,SmallIcon("view_tree"), i18n("Projects"));
00198   projectlist = new KateProjectList (this, prBox/*filelistDock*/, "projectlist");
00199   projectviews = new KateProjectViews (this, prBox/*filelistDock*/, "projectviews");
00200   prBox->setStretchFactor(projectviews, 2);
00201   prBox->show ();
00202   projectlist->show ();
00203   projectviews->show ();
00204 
00205   fileselector = new KateFileSelector( this, m_viewManager, /*fileselectorDock*/ this, "operator");
00206   addToolView(KDockWidget::DockLeft,fileselector, SmallIcon("fileopen"), i18n("Filesystem Browser"));
00207 
00208   // TEST
00209   addToolView( KDockWidget::DockBottom, greptool, SmallIcon("filefind"), i18n("Find in Files") );
00210   if (kapp->authorize("shell_access"))
00211   {
00212      console = new KateConsole (this, "console",viewManager());
00213      console->installEventFilter( this );
00214      addToolView(KDockWidget::DockBottom,console, SmallIcon("konsole"), i18n("Terminal"));
00215   }
00216 
00217   connect(fileselector->dirOperator(),SIGNAL(fileSelected(const KFileItem*)),this,SLOT(fileSelected(const KFileItem*)));
00218 }
00219 
00220 void KateMainWindow::setupActions()
00221 {
00222   KAction *a;
00223 
00224   kscript = new KScriptManager(this, "scriptmanager");
00225   scriptMenu = new KActionMenu( i18n("&KDE Scripts"), actionCollection(), "scripts");
00226   scriptMenu->setWhatsThis(i18n("This shows all available scripts and allows them to be executed."));
00227   setupScripts();
00228   connect( scriptMenu->popupMenu(), SIGNAL(activated( int)), this, SLOT(runScript( int )) );
00229 
00230   KStdAction::openNew( m_viewManager, SLOT( slotDocumentNew() ), actionCollection(), "file_new" )->setWhatsThis(i18n("Create a new document"));
00231   KStdAction::open( m_viewManager, SLOT( slotDocumentOpen() ), actionCollection(), "file_open" )->setWhatsThis(i18n("Open an existing document for editing"));
00232 
00233   fileOpenRecent = KStdAction::openRecent (m_viewManager, SLOT(openURL (const KURL&)), actionCollection());
00234   fileOpenRecent->setWhatsThis(i18n("This lists files which you have opened recently, and allows you to easily open them again."));
00235 
00236   a=new KAction( i18n("Save A&ll"),"save_all", CTRL+Key_L, KateDocManager::self(), SLOT( saveAll() ), actionCollection(), "file_save_all" );
00237   a->setWhatsThis(i18n("Save all open, modified documents to disk."));
00238 
00239   KStdAction::close( m_viewManager, SLOT( slotDocumentClose() ), actionCollection(), "file_close" )->setWhatsThis(i18n("Close the current document."));
00240 
00241   a=new KAction( i18n( "Clos&e All" ), 0, this, SLOT( slotDocumentCloseAll() ), actionCollection(), "file_close_all" );
00242   a->setWhatsThis(i18n("Close all open documents."));
00243 
00244   KStdAction::mail( this, SLOT(slotMail()), actionCollection() )->setWhatsThis(i18n("Send one or more of the open documents as email attachments."));
00245 
00246   KStdAction::quit( this, SLOT( slotFileQuit() ), actionCollection(), "file_quit" )->setWhatsThis(i18n("Close this window"));
00247 
00248   a=new KAction(i18n("&New Window"), "window_new", 0, this, SLOT(newWindow()), actionCollection(), "view_new_view");
00249   a->setWhatsThis(i18n("Create a new Kate view (a new window with the same document list)."));
00250 
00251   externalTools = new KateExternalToolsMenuAction( i18n("External Tools"), actionCollection(), "tools_external", this );
00252   externalTools->setWhatsThis( i18n("Launch external helper applications") );
00253 
00254   showFullScreenAction = KStdAction::fullScreen( 0, 0, actionCollection(),this);
00255   connect( showFullScreenAction,SIGNAL(toggled(bool)), this,SLOT(slotFullScreen(bool)));
00256 
00257   documentOpenWith = new KActionMenu(i18n("Open W&ith"), actionCollection(), "file_open_with");
00258   documentOpenWith->setWhatsThis(i18n("Open the current document using another application registered for its file type, or an application of your choice."));
00259   connect(documentOpenWith->popupMenu(), SIGNAL(aboutToShow()), this, SLOT(mSlotFixOpenWithMenu()));
00260   connect(documentOpenWith->popupMenu(), SIGNAL(activated(int)), this, SLOT(slotOpenWithMenuAction(int)));
00261 
00262   a=KStdAction::keyBindings(this, SLOT(editKeys()), actionCollection());
00263   a->setWhatsThis(i18n("Configure the application's keyboard shortcut assignments."));
00264 
00265   a=KStdAction::configureToolbars(this, SLOT(slotEditToolbars()), actionCollection(), "set_configure_toolbars");
00266   a->setWhatsThis(i18n("Configure which items should appear in the toolbar(s)."));
00267 
00268   // project menu
00269   a = new KAction(i18n("&New Project..."), "filenew", 0, this, SLOT(slotProjectNew()), actionCollection(), "project_new");
00270   a = new KAction(i18n("&Open Project..."), "fileopen", 0, this, SLOT(slotProjectOpen()), actionCollection(), "project_open");
00271   saveProject = new KAction(i18n("&Save Project"), "filesave", 0, this, SLOT(slotProjectSave()), actionCollection(), "project_save");
00272   closeProject = new KAction(i18n("&Close Project"), "fileclose", 0, this, SLOT(slotProjectClose()), actionCollection(), "project_close");
00273 
00274   recentProjects = new KRecentFilesAction (i18n("Open &Recent"), KShortcut(), this, SLOT(openConstURLProject (const KURL&)),actionCollection(), "project_open_recent");
00275 
00276   settingsConfigure = KStdAction::preferences(this, SLOT(slotConfigure()), actionCollection(), "settings_configure");
00277   settingsConfigure->setWhatsThis(i18n("Configure various aspects of this application and the editing component."));
00278 
00279   // pipe to terminal action
00280   if (kapp->authorize("shell_access"))
00281     new KAction(i18n("&Pipe to Console"), "pipe", 0, this, SLOT(slotPipeToConsole()), actionCollection(), "tools_pipe_to_terminal");
00282 
00283   // tip of the day :-)
00284   KStdAction::tipOfDay( this, SLOT( tipOfTheDay() ), actionCollection() )->setWhatsThis(i18n("This shows useful tips on the use of this application."));
00285 
00286   if (KatePluginManager::self()->pluginList().count() > 0)
00287   {
00288     a=new KAction(i18n("&Plugins Handbook"), 0, this, SLOT(pluginHelp()), actionCollection(), "help_plugins_contents");
00289     a->setWhatsThis(i18n("This shows help files for various available plugins."));
00290   }
00291 
00292   connect(m_viewManager,SIGNAL(viewChanged()),this,SLOT(slotWindowActivated()));
00293   connect(m_viewManager,SIGNAL(viewChanged()),this,SLOT(slotUpdateOpenWith()));
00294 
00295   slotWindowActivated ();
00296 
00297   projectlist->setupActions();
00298 }
00299 
00300 void KateMainWindow::slotDocumentCloseAll() {
00301   if (queryClose_internal())
00302     KateDocManager::self()->closeAllDocuments(false);
00303 }
00304 
00305 bool KateMainWindow::queryClose_internal() {
00306    uint documentCount=KateDocManager::self()->documents();
00307 
00308   if ( ! showModOnDiskPrompt() )
00309     return false;
00310 
00311   QPtrList<Kate::Document> modifiedDocuments=KateDocManager::self()->modifiedDocumentList();
00312   bool shutdown=(modifiedDocuments.count()==0);
00313 
00314   if (!shutdown) {
00315     shutdown=KateSaveModifiedDialog::queryClose(this,modifiedDocuments);
00316   }
00317 
00318   if ( KateDocManager::self()->documents() > documentCount ) {
00319     KMessageBox::information (this,
00320                               i18n ("New file opened while trying to close Kate, closing aborted."),
00321                               i18n ("Closing Aborted"));
00322     shutdown=false;
00323   }
00324 
00325   return shutdown;
00326 }
00327 
00331 bool KateMainWindow::queryClose()
00332 {
00333   // session saving, can we close all projects & views ?
00334   // just test, not close them actually
00335   if (kapp->sessionSaving())
00336   {
00337     return ( KateProjectManager::self()->queryCloseAll () &&
00338              queryClose_internal() );
00339   }
00340 
00341   // normal closing of window
00342   // allow to close all windows until the last without restrictions
00343   if ( ((KateApp *)kapp)->mainWindows () > 1 )
00344     return true;
00345 
00346   // last one: check if we can close all projects/document, try run
00347   // and save projects/docs if we really close down !
00348 
00349 
00350   if ( KateProjectManager::self()->queryCloseAll () &&
00351        queryClose_internal() )
00352   {
00353     KConfig scfg("katesessionrc", false);
00354 
00355     KConfig *config = kapp->config();
00356     config->setGroup("General");
00357 
00358     if (config->readBoolEntry("Restore Projects", false))
00359       KateProjectManager::self()->saveProjectList (&scfg);
00360 
00361     if (config->readBoolEntry("Restore Documents", false))
00362       KateDocManager::self()->saveDocumentList (&scfg);
00363 
00364     if (config->readBoolEntry("Restore Window Configuration", false))
00365       saveProperties (&scfg);
00366 
00367     return true;
00368   }
00369 
00370   return false;
00371 }
00372 
00373 void KateMainWindow::newWindow ()
00374 {
00375   ((KateApp *)kapp)->newMainWindow ();
00376 }
00377 
00378 void KateMainWindow::slotEditToolbars()
00379 {
00380   KEditToolbar dlg( factory() );
00381 
00382   dlg.exec();
00383 }
00384 
00385 void KateMainWindow::slotFileQuit()
00386 {
00387   close ();
00388 }
00389 
00390 void KateMainWindow::readOptions(KConfig *config)
00391 {
00392   config->setGroup("General");
00393   syncKonsole =  config->readBoolEntry("Sync Konsole", true);
00394   modNotification = config->readBoolEntry("Modified Notification", false);
00395   KateDocManager::self()->setSaveMetaInfos(config->readBoolEntry("Save Meta Infos", true));
00396   KateDocManager::self()->setDaysMetaInfos(config->readNumEntry("Days Meta Infos", 30));
00397 
00398   m_viewManager->setShowFullPath(config->readBoolEntry("Show Full Path in Title", false));
00399 
00400   fileOpenRecent->loadEntries(config, "Recent Files");
00401 
00402   fileselector->readConfig(config, "fileselector");
00403 
00404   recentProjects->loadEntries (config, "Recent Projects");
00405 }
00406 
00407 void KateMainWindow::saveOptions(KConfig *config)
00408 {
00409   saveMainWindowSettings(config, "Kate Main Window");
00410 
00411   config->setGroup("General");
00412 
00413   if (console)
00414     config->writeEntry("Show Console", console->isVisible());
00415   else
00416     config->writeEntry("Show Console", false);
00417 
00418   config->writeEntry("Save Meta Infos", KateDocManager::self()->getSaveMetaInfos());
00419 
00420   config->writeEntry("Days Meta Infos", KateDocManager::self()->getDaysMetaInfos());
00421 
00422   config->writeEntry("Show Full Path in Title", m_viewManager->getShowFullPath());
00423 
00424   config->writeEntry("Sync Konsole", syncKonsole);
00425 
00426   fileOpenRecent->saveEntries(config, "Recent Files");
00427 
00428   fileselector->writeConfig(config, "fileselector");
00429 
00430   filelist->writeConfig(config, "Filelist");
00431 
00432   recentProjects->saveEntries (config, "Recent Projects");
00433 }
00434 
00435 void KateMainWindow::slotWindowActivated ()
00436 {
00437   static QString path;
00438 
00439   if (m_viewManager->activeView())
00440   {
00441     if (console && syncKonsole)
00442     {
00443       QString newPath = m_viewManager->activeView()->getDoc()->url().directory();
00444 
00445       if ( newPath != path )
00446       {
00447         path = newPath;
00448         console->cd (KURL( path ));
00449       }
00450     }
00451 
00452     updateCaption (m_viewManager->activeView()->getDoc());
00453   }
00454 }
00455 
00456 void KateMainWindow::slotUpdateOpenWith()
00457 {
00458   if (m_viewManager->activeView())
00459     documentOpenWith->setEnabled(!m_viewManager->activeView()->document()->url().isEmpty());
00460   else
00461     documentOpenWith->setEnabled(false);
00462 }
00463 
00464 void KateMainWindow::documentMenuAboutToShow()
00465 {
00466   documentMenu->clear ();
00467 
00468   QListViewItem * item = filelist->firstChild();
00469   while( item ) {
00470     documentMenu->insertItem (
00471       KStringHandler::rsqueeze( ((KateFileListItem *)item)->document()->docName(), 150 ), // would it be saner to use the screen width as a limit that some random number??
00472           m_viewManager, SLOT (activateView (int)), 0,
00473       ((KateFileListItem *)item)->documentNumber () );
00474 
00475     item = item->nextSibling();
00476   }
00477   if (m_viewManager->activeView())
00478     documentMenu->setItemChecked ( m_viewManager->activeView()->getDoc()->documentNumber(), true);
00479 }
00480 
00481 void KateMainWindow::slotGrepToolItemSelected(const QString &filename,int linenumber)
00482 {
00483   KURL fileURL;
00484   fileURL.setPath( filename );
00485   m_viewManager->openURL( fileURL );
00486   if ( m_viewManager->activeView() == 0 ) return;
00487   m_viewManager->activeView()->gotoLineNumber( linenumber );
00488   raise();
00489   setActiveWindow();
00490 }
00491 
00492 void KateMainWindow::dragEnterEvent( QDragEnterEvent *event )
00493 {
00494   event->accept(KURLDrag::canDecode(event));
00495 }
00496 
00497 void KateMainWindow::dropEvent( QDropEvent *event )
00498 {
00499   slotDropEvent(event);
00500 }
00501 
00502 void KateMainWindow::slotDropEvent( QDropEvent * event )
00503 {
00504   KURL::List textlist;
00505   if (!KURLDrag::decode(event, textlist)) return;
00506 
00507   for (KURL::List::Iterator i=textlist.begin(); i != textlist.end(); ++i)
00508   {
00509     m_viewManager->openURL (*i);
00510   }
00511 }
00512 
00513 void KateMainWindow::editKeys()
00514 {
00515   KKeyDialog dlg ( false, this );
00516 
00517   QPtrList<KXMLGUIClient> clients = guiFactory()->clients();
00518 
00519   for( QPtrListIterator<KXMLGUIClient> it( clients ); it.current(); ++it )
00520     dlg.insert ( (*it)->actionCollection(), (*it)->instance()->aboutData()->programName() );
00521 
00522   dlg.insert( externalTools->actionCollection(), i18n("External Tools") );
00523 
00524   dlg.configure();
00525 
00526   QPtrList<Kate::Document>  l=KateDocManager::self()->documentList();
00527   for (uint i=0;i<l.count();i++) {
00528 //     kdDebug(13001)<<"reloading Keysettings for document "<<i<<endl;
00529     l.at(i)->reloadXML();
00530     QPtrList<class KTextEditor::View> l1=l.at(i)->views ();//KTextEditor::Document
00531     for (uint i1=0;i1<l1.count();i1++) {
00532       l1.at(i1)->reloadXML();
00533 //       kdDebug(13001)<<"reloading Keysettings for view "<<i<<"/"<<i1<<endl;
00534     }
00535   }
00536 
00537   externalTools->actionCollection()->writeShortcutSettings( "Shortcuts", new KConfig("externaltools", false, false, "appdata") );
00538 }
00539 
00540 void KateMainWindow::openURL (const QString &name)
00541 {
00542   m_viewManager->openURL (KURL(name));
00543 }
00544 
00545 void KateMainWindow::slotConfigure()
00546 {
00547   if (!m_viewManager->activeView())
00548     return;
00549 
00550   KateConfigDialog* dlg = new KateConfigDialog (this, m_viewManager->activeView());
00551   dlg->exec();
00552 
00553   delete dlg;
00554 }
00555 
00556 KURL KateMainWindow::activeDocumentUrl()
00557 {
00558   // anders: i make this one safe, as it may be called during
00559   // startup (by the file selector)
00560   Kate::View *v = m_viewManager->activeView();
00561   if ( v )
00562     return v->getDoc()->url();
00563   return KURL();
00564 }
00565 
00566 void KateMainWindow::fileSelected(const KFileItem * /*file*/)
00567 {
00568   /*if (file) {
00569     m_viewManager->openURL( file->url() );
00570  } else */{
00571     const KFileItemList *list=fileselector->dirOperator()->selectedItems();
00572     KFileItem *tmp;
00573     for (KFileItemListIterator it(*list); (tmp = it.current()); ++it) {
00574         m_viewManager->openURL(tmp->url());
00575         fileselector->dirOperator()->view()->setSelected(tmp,false);
00576     }
00577  }
00578     //fileSelector->dirOperator()->
00579 }
00580 
00581 // TODO make this work
00582 void KateMainWindow::mSlotFixOpenWithMenu()
00583 {
00584   //kdDebug(13001)<<"13000"<<"fixing open with menu"<<endl;
00585   documentOpenWith->popupMenu()->clear();
00586   // get a list of appropriate services.
00587   KMimeType::Ptr mime = KMimeType::findByURL( m_viewManager->activeView()->getDoc()->url() );
00588   //kdDebug(13001)<<"13000"<<"url: "<<m_viewManager->activeView()->getDoc()->url().prettyURL()<<"mime type: "<<mime->name()<<endl;
00589   // some checking goes here...
00590   KTrader::OfferList offers = KTrader::self()->query(mime->name(), "Type == 'Application'");
00591   // for each one, insert a menu item...
00592   for(KTrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it) {
00593     if ((*it)->name() == "Kate") continue;
00594     documentOpenWith->popupMenu()->insertItem( SmallIcon( (*it)->icon() ), (*it)->name() );
00595   }
00596   // append "Other..." to call the KDE "open with" dialog.
00597   documentOpenWith->popupMenu()->insertItem(i18n("&Other..."));
00598 }
00599 
00600 void KateMainWindow::slotOpenWithMenuAction(int idx)
00601 {
00602   KURL::List list;
00603   list.append( m_viewManager->activeView()->getDoc()->url() );
00604   QString* appname = new QString( documentOpenWith->popupMenu()->text(idx) );
00605   if ( appname->compare(i18n("&Other...")) == 0 ) {
00606     // display "open with" dialog
00607     KOpenWithDlg* dlg = new KOpenWithDlg(list);
00608     if (dlg->exec())
00609       KRun::run(*dlg->service(), list);
00610     return;
00611   }
00612   QString qry = QString("((Type == 'Application') and (Name == '%1'))").arg( appname->latin1() );
00613   KMimeType::Ptr mime = KMimeType::findByURL( m_viewManager->activeView()->getDoc()->url() );
00614   KTrader::OfferList offers = KTrader::self()->query(mime->name(), qry);
00615   KService::Ptr app = offers.first();
00616   // some checking here: pop a wacko message it the app wasn't found.
00617   KRun::run(*app, list);
00618 }
00619 
00620 void KateMainWindow::pluginHelp()
00621 {
00622   kapp->invokeHelp (QString::null, "kate-plugins");
00623 }
00624 
00625 void KateMainWindow::setupScripts()
00626 {
00627   // locate all scripts, local as well as global.
00628   // The script manager will do the nessecary sanity checking
00629   QStringList scripts = KGlobal::dirs()->findAllResources("data", QString(kapp->name())+"/scripts/*.desktop", false, true );
00630   for (QStringList::Iterator it = scripts.begin(); it != scripts.end(); ++it )
00631     kscript->addScript( *it );
00632   QStringList l ( kscript->scripts() );
00633   for (QStringList::Iterator it=l.begin(); it != l.end(); ++it )
00634     scriptMenu->popupMenu()->insertItem( *it );
00635 }
00636 
00637 void KateMainWindow::runScript( int mIId )
00638 {
00639     //kdDebug(13001) << "Starting script engine..." << endl;
00640 //         kdDebug(13001)<<"runScript( "<<mIId<<" ) ["<<scriptMenu->popupMenu()->text( mIId )<<"]"<<endl;
00641     kscript->runScript( scriptMenu->popupMenu()->text( mIId ) );
00642 }
00643 
00644 void KateMainWindow::slotMail()
00645 {
00646   KateMailDialog *d = new KateMailDialog(this, this);
00647   if ( ! d->exec() )
00648     return;
00649   QPtrList<Kate::Document> attDocs = d->selectedDocs();
00650   delete d;
00651   // Check that all selected files are saved (or shouldn't be)
00652   QStringList urls; // to atthatch
00653   Kate::Document *doc;
00654   QPtrListIterator<Kate::Document> it(attDocs);
00655   for ( ; it.current(); ++it ) {
00656     doc = it.current();
00657     if (!doc) continue;
00658     if ( doc->url().isEmpty() ) {
00659       // unsaved document. back out unless it gets saved
00660       int r = KMessageBox::questionYesNo( this,
00661               i18n("<p>The current document has not been saved, and "
00662               "cannot be attached to an email message."
00663               "<p>Do you want to save it and proceed?"),
00664               i18n("Cannot Send Unsaved File") );
00665       if ( r == KMessageBox::Yes ) {
00666         Kate::View *v = (Kate::View*)doc->views().first();
00667         int sr = v->saveAs();
00668         if ( sr == Kate::View::SAVE_OK ) { ;
00669         }
00670         else {
00671           if ( sr != Kate::View::SAVE_CANCEL ) // ERROR or RETRY(?)
00672             KMessageBox::sorry( this, i18n("The file could not be saved. Please check "
00673                                         "if you have write permission.") );
00674           continue;
00675         }
00676       }
00677       else
00678         continue;
00679     }
00680     if ( doc->isModified() ) {
00681       // warn that document is modified and offer to save it before proceeding.
00682       int r = KMessageBox::warningYesNoCancel( this,
00683                 i18n("<p>The current file:<br><strong>%1</strong><br>has been "
00684                 "modified. Modifications will not be available in the attachment."
00685                 "<p>Do you want to save it before sending it?").arg(doc->url().prettyURL()),
00686                 i18n("Save Before Sending?") );
00687       switch ( r ) {
00688         case KMessageBox::Cancel:
00689           continue;
00690         case KMessageBox::Yes:
00691           doc->save();
00692           if ( doc->isModified() ) { // read-only docs ends here, if modified. Hmm.
00693             KMessageBox::sorry( this, i18n("The file could not be saved. Please check "
00694                                       "if you have write permission.") );
00695             continue;
00696           }
00697           break;
00698         default:
00699           break;
00700       }
00701     }
00702     // finally call the mailer
00703     urls << doc->url().url();
00704   } // check selected docs done
00705   if ( ! urls.count() )
00706     return;
00707   kapp->invokeMailer( QString::null, // to
00708                       QString::null, // cc
00709                       QString::null, // bcc
00710                       QString::null, // subject
00711                       QString::null, // body
00712                       QString::null, // msgfile
00713                       urls           // urls to atthatch
00714                       );
00715 }
00716 void KateMainWindow::tipOfTheDay()
00717 {
00718   KTipDialog::showTip( /*0*/this, QString::null, true );
00719 }
00720 
00721 void KateMainWindow::slotFullScreen(bool t)
00722 {
00723   if (t)
00724     showFullScreen();
00725   else
00726     showNormal();
00727 }
00728 
00729 bool KateMainWindow::eventFilter( QObject *o, QEvent *e )
00730 {
00731 
00732   if ( o == greptool && e->type() == QEvent::Show && m_viewManager->activeView() )
00733   {
00734     if ( m_viewManager->activeView()->getDoc()->url().isLocalFile() )
00735     {
00736       greptool->updateDirName( m_viewManager->activeView()->getDoc()->url().directory() );
00737       return true;
00738     }
00739   }
00740   if ( ( o == greptool || o == console ) &&
00741       e->type() == QEvent::Hide && m_viewManager->activeView() )
00742   {
00743      m_viewManager->activeView()->setFocus();
00744      return true;
00745   }
00746 
00747   return KMDI::MainWindow::eventFilter( o, e );
00748 }
00749 
00750 bool KateMainWindow::event( QEvent *e )
00751 {
00752   uint type = e->type();
00753   if ( type == QEvent::WindowActivate && modNotification )
00754   {
00755     if ( m_modignore )
00756     {
00757       m_modignore = false;
00758       return KMDI::MainWindow::event( e );
00759     }
00760     showModOnDiskPrompt();
00761   }
00762   // Try to disable the modonhd prompt from showing after internal dialogs.
00763   // TODO make this work better.
00764   else if ( (type == QEvent::WindowUnblocked || type == QEvent::WindowBlocked) && modNotification)
00765     m_modignore = true;
00766 
00767   return KMDI::MainWindow::event( e );
00768 }
00769 
00770 bool KateMainWindow::showModOnDiskPrompt()
00771 {
00772   Kate::Document *doc;
00773 
00774   DocVector list( KateDocManager::self()->documents() );
00775   uint cnt = 0;
00776   for( doc = KateDocManager::self()->firstDocument(); doc; doc = KateDocManager::self()->nextDocument() )
00777   {
00778     if ( KateDocManager::self()->documentInfo( doc )->modifiedOnDisc )
00779     {
00780       list.insert( cnt, doc );
00781       cnt++;
00782     }
00783   }
00784 
00785   if ( cnt )
00786   {
00787     list.resize( cnt );
00788     KateMwModOnHdDialog mhdlg( list, this );
00789     bool res = mhdlg.exec();
00790 
00791     return res;
00792   }
00793   return true;
00794 }
00795 
00796 KMDI::ToolViewAccessor *KateMainWindow::addToolView(KDockWidget::DockPosition position, QWidget *widget, const QPixmap &icon, const QString &sname, const QString &tabToolTip, const QString &tabCaption)
00797 {
00798   widget->setIcon(icon);
00799   widget->setCaption(sname);
00800 
00801   return addToolWindow(widget, position, getMainDockWidget(), 25, tabToolTip, tabCaption);
00802 }
00803 
00804 bool KateMainWindow::removeToolView(QWidget *w)
00805 {
00806   deleteToolWindow (w);
00807   return true;
00808 }
00809 
00810 bool KateMainWindow::removeToolView(KMDI::ToolViewAccessor *accessor)
00811 {
00812   deleteToolWindow (accessor);
00813   return true;
00814 }
00815 
00816 bool KateMainWindow::showToolView(QWidget *){return false;}
00817 bool KateMainWindow::showToolView(KMDI::ToolViewAccessor *){return false;}
00818 
00819 bool KateMainWindow::hideToolView(QWidget *){return false;}
00820 bool KateMainWindow::hideToolView(KMDI::ToolViewAccessor *){return false;}
00821 
00822 void KateMainWindow::slotProjectNew ()
00823 {
00824   ProjectInfo *info = KateProjectManager::self()->newProjectDialog (this);
00825 
00826   if (info)
00827   {
00828     createProject (info->type, info->name, info->fileName);
00829     delete info;
00830   }
00831 }
00832 
00833 void KateMainWindow::slotProjectOpen ()
00834 {
00835   QString fileName = KFileDialog::getOpenFileName (QString::null, QString ("*.kateproject|") + i18n("Kate Project Files") + QString (" (*.kateproject)"), this, i18n("Open Kate Project"));
00836 
00837   if (!fileName.isEmpty())
00838     openProject (fileName);
00839 }
00840 
00841 void KateMainWindow::slotProjectSave ()
00842 {
00843   if (m_project)
00844     m_project->save ();
00845 }
00846 
00847 void KateMainWindow::slotProjectClose ()
00848 {
00849   if (m_project)
00850   {
00851     KateProjectManager::self()->close (m_project);
00852   }
00853 }
00854 
00855 void KateMainWindow::activateProject (Kate::Project *project)
00856 {
00857 //   kdDebug(13001)<<"activating project "<<project<<endl;
00858   if (m_project)
00859     KateProjectManager::self()->disableProjectGUI (m_project, this);
00860 
00861   if (project)
00862     KateProjectManager::self()->enableProjectGUI (project, this);
00863 
00864   m_project = project;
00865 
00866   if (project)
00867   {
00868     KateProjectManager::self()->setCurrentProject (project);
00869     m_projectNumber = project->projectNumber ();
00870   }
00871   else
00872     m_projectNumber = 0;
00873 
00874   saveProject->setEnabled(project != 0);
00875   closeProject->setEnabled(project != 0);
00876 
00877   emit m_mainWindow->projectChanged ();
00878 }
00879 
00880 Kate::Project *KateMainWindow::createProject (const QString &type, const QString &name, const QString &filename)
00881 {
00882   Kate::Project *project = KateProjectManager::self()->create (type, name, filename);
00883 
00884   if (project)
00885     activateProject (project);
00886 
00887   return project;
00888 }
00889 
00890 Kate::Project *KateMainWindow::openProject (const QString &filename)
00891 {
00892   Kate::Project *project = KateProjectManager::self()->open (filename);
00893 
00894   if (project)
00895   {
00896     recentProjects->addURL ( KURL(filename) );
00897     activateProject (project);
00898   }
00899 
00900   return project;
00901 }
00902 
00903 void KateMainWindow::projectDeleted (uint projectNumber)
00904 {
00905   if (projectNumber == m_projectNumber)
00906   {
00907     if (KateProjectManager::self()->projects() > 0)
00908       activateProject (KateProjectManager::self()->project(KateProjectManager::self()->projects()-1));
00909     else
00910       activateProject (0);
00911   }
00912 }
00913 
00914 void KateMainWindow::slotDocumentCreated (Kate::Document *doc)
00915 {
00916   connect(doc,SIGNAL(modStateChanged(Kate::Document *)),this,SLOT(updateCaption(Kate::Document *)));
00917   connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(updateCaption(Kate::Document *)));
00918   connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(slotUpdateOpenWith()));
00919 
00920   updateCaption (doc);
00921 }
00922 
00923 void KateMainWindow::updateCaption (Kate::Document *doc)
00924 {
00925   if (!m_viewManager->activeView())
00926   {
00927     setCaption ("", false);
00928     return;
00929   }
00930 
00931   if (!(m_viewManager->activeView()->getDoc() == doc))
00932     return;
00933 
00934   QString c;
00935   if (m_viewManager->activeView()->getDoc()->url().isEmpty() || (!m_viewManager->getShowFullPath()))
00936   {
00937     c = m_viewManager->activeView()->getDoc()->docName();
00938   }
00939   else
00940   {
00941     c = m_viewManager->activeView()->getDoc()->url().prettyURL();
00942   }
00943 
00944   setCaption( KStringHandler::lsqueeze(c,64), m_viewManager->activeView()->getDoc()->isModified());
00945 }
00946 
00947 void KateMainWindow::openConstURLProject (const KURL&url)
00948 {
00949   openProject (url.path());
00950 }
00951 
00952 void KateMainWindow::saveProperties(KConfig *config) {
00953   kdDebug(13000)<<"KateMainWindow::saveProperties(): group: "<<config->group()<<endl;
00954   assert(config);
00955 
00956 //   kdDebug(13000)<<"preparing session saving"<<endl;
00957   QString grp=config->group();
00958   QString dockGrp;
00959 
00960   if (kapp->sessionSaving()) dockGrp=grp+"-Docking";
00961     else dockGrp="MainWindow0-Docking";
00962 /*  if (config->readNumEntry("GUIMode",KMdi::UndefinedMode)!=mdiMode()) {
00963         config->writeEntry("GUIMode",mdiMode());
00964         config->deleteGroup("MainWindow0-Docking");
00965   }*/
00966 
00967 //   kdDebug(13000)<<"Before write dock config"<<endl;
00968   writeDockConfig(config,dockGrp);
00969 //   kdDebug(13000)<<"After write dock config"<<endl;
00970 
00971 
00972   if (kapp->sessionSaving()) dockGrp=grp+"-View Configuration";
00973     else dockGrp="MainWindow0-View Configuration";
00974 
00975   m_viewManager->saveViewConfiguration (config,dockGrp);
00976 //   kdDebug(13000)<<"After saving view configuration"<<endl;
00977   config->setGroup(grp);
00978 
00979 }
00980 
00981 void KateMainWindow::readProperties(KConfig *config)
00982 {
00983   QString grp=config->group();
00984   QString dockGrp;
00985 
00986   if (kapp->isRestored()) dockGrp=grp+"-Docking";
00987     else dockGrp="MainWindow0-Docking";
00988 
00989   if (config->hasGroup(dockGrp))
00990         readDockConfig(config,dockGrp);
00991 
00992   if (kapp->isRestored()) dockGrp=grp+"-View Configuration";
00993     else dockGrp="MainWindow0-View Configuration";
00994 
00995   m_viewManager->restoreViewConfiguration (config,dockGrp);
00996   config->setGroup(grp);
00997 }
00998 
00999 void KateMainWindow::saveGlobalProperties( KConfig* sessionConfig )
01000 {
01001   KateProjectManager::self()->saveProjectList (sessionConfig);
01002   KateDocManager::self()->saveDocumentList (sessionConfig);
01003 }
01004 
01005 void KateMainWindow::slotPipeToConsole ()
01006 {
01007   if (!console)
01008     return;
01009 
01010   Kate::View *v = m_viewManager->activeView();
01011 
01012   if (!v)
01013     return;
01014 
01015   if (v->getDoc()->hasSelection ())
01016     console->sendInput (v->getDoc()->selection());
01017   else
01018     console->sendInput (v->getDoc()->text());
01019 }
01020 // 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 Fri Nov 4 00:45:19 2005 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003