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   // remove documents (not: Next/Prev/Separator)
00467   while (documentMenu->count() > 3)
00468     documentMenu->removeItemAt (3);
00469 
00470   QListViewItem * item = filelist->firstChild();
00471   while( item ) {
00472     documentMenu->insertItem (
00473       KStringHandler::rsqueeze( ((KateFileListItem *)item)->document()->docName(), 150 ), // would it be saner to use the screen width as a limit that some random number??
00474           m_viewManager, SLOT (activateView (int)), 0,
00475       ((KateFileListItem *)item)->documentNumber () );
00476 
00477     item = item->nextSibling();
00478   }
00479   if (m_viewManager->activeView())
00480     documentMenu->setItemChecked ( m_viewManager->activeView()->getDoc()->documentNumber(), true);
00481 }
00482 
00483 void KateMainWindow::slotGrepToolItemSelected(const QString &filename,int linenumber)
00484 {
00485   KURL fileURL;
00486   fileURL.setPath( filename );
00487   m_viewManager->openURL( fileURL );
00488   if ( m_viewManager->activeView() == 0 ) return;
00489   m_viewManager->activeView()->gotoLineNumber( linenumber );
00490   raise();
00491   setActiveWindow();
00492 }
00493 
00494 void KateMainWindow::dragEnterEvent( QDragEnterEvent *event )
00495 {
00496   event->accept(KURLDrag::canDecode(event));
00497 }
00498 
00499 void KateMainWindow::dropEvent( QDropEvent *event )
00500 {
00501   slotDropEvent(event);
00502 }
00503 
00504 void KateMainWindow::slotDropEvent( QDropEvent * event )
00505 {
00506   KURL::List textlist;
00507   if (!KURLDrag::decode(event, textlist)) return;
00508 
00509   for (KURL::List::Iterator i=textlist.begin(); i != textlist.end(); ++i)
00510   {
00511     m_viewManager->openURL (*i);
00512   }
00513 }
00514 
00515 void KateMainWindow::editKeys()
00516 {
00517   KKeyDialog dlg ( false, this );
00518 
00519   QPtrList<KXMLGUIClient> clients = guiFactory()->clients();
00520 
00521   for( QPtrListIterator<KXMLGUIClient> it( clients ); it.current(); ++it )
00522     dlg.insert ( (*it)->actionCollection(), (*it)->instance()->aboutData()->programName() );
00523 
00524   dlg.insert( externalTools->actionCollection(), i18n("External Tools") );
00525 
00526   dlg.configure();
00527 
00528   QPtrList<Kate::Document>  l=KateDocManager::self()->documentList();
00529   for (uint i=0;i<l.count();i++) {
00530 //     kdDebug(13001)<<"reloading Keysettings for document "<<i<<endl;
00531     l.at(i)->reloadXML();
00532     QPtrList<class KTextEditor::View> l1=l.at(i)->views ();//KTextEditor::Document
00533     for (uint i1=0;i1<l1.count();i1++) {
00534       l1.at(i1)->reloadXML();
00535 //       kdDebug(13001)<<"reloading Keysettings for view "<<i<<"/"<<i1<<endl;
00536     }
00537   }
00538 
00539   externalTools->actionCollection()->writeShortcutSettings( "Shortcuts", new KConfig("externaltools", false, false, "appdata") );
00540 }
00541 
00542 void KateMainWindow::openURL (const QString &name)
00543 {
00544   m_viewManager->openURL (KURL(name));
00545 }
00546 
00547 void KateMainWindow::slotConfigure()
00548 {
00549   if (!m_viewManager->activeView())
00550     return;
00551 
00552   KateConfigDialog* dlg = new KateConfigDialog (this, m_viewManager->activeView());
00553   dlg->exec();
00554 
00555   delete dlg;
00556 }
00557 
00558 KURL KateMainWindow::activeDocumentUrl()
00559 {
00560   // anders: i make this one safe, as it may be called during
00561   // startup (by the file selector)
00562   Kate::View *v = m_viewManager->activeView();
00563   if ( v )
00564     return v->getDoc()->url();
00565   return KURL();
00566 }
00567 
00568 void KateMainWindow::fileSelected(const KFileItem * /*file*/)
00569 {
00570   /*if (file) {
00571     m_viewManager->openURL( file->url() );
00572  } else */{
00573     const KFileItemList *list=fileselector->dirOperator()->selectedItems();
00574     KFileItem *tmp;
00575     for (KFileItemListIterator it(*list); (tmp = it.current()); ++it) {
00576         m_viewManager->openURL(tmp->url());
00577         fileselector->dirOperator()->view()->setSelected(tmp,false);
00578     }
00579  }
00580     //fileSelector->dirOperator()->
00581 }
00582 
00583 // TODO make this work
00584 void KateMainWindow::mSlotFixOpenWithMenu()
00585 {
00586   //kdDebug(13001)<<"13000"<<"fixing open with menu"<<endl;
00587   documentOpenWith->popupMenu()->clear();
00588   // get a list of appropriate services.
00589   KMimeType::Ptr mime = KMimeType::findByURL( m_viewManager->activeView()->getDoc()->url() );
00590   //kdDebug(13001)<<"13000"<<"url: "<<m_viewManager->activeView()->getDoc()->url().prettyURL()<<"mime type: "<<mime->name()<<endl;
00591   // some checking goes here...
00592   KTrader::OfferList offers = KTrader::self()->query(mime->name(), "Type == 'Application'");
00593   // for each one, insert a menu item...
00594   for(KTrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it) {
00595     if ((*it)->name() == "Kate") continue;
00596     documentOpenWith->popupMenu()->insertItem( SmallIcon( (*it)->icon() ), (*it)->name() );
00597   }
00598   // append "Other..." to call the KDE "open with" dialog.
00599   documentOpenWith->popupMenu()->insertItem(i18n("&Other..."));
00600 }
00601 
00602 void KateMainWindow::slotOpenWithMenuAction(int idx)
00603 {
00604   KURL::List list;
00605   list.append( m_viewManager->activeView()->getDoc()->url() );
00606   QString* appname = new QString( documentOpenWith->popupMenu()->text(idx) );
00607   if ( appname->compare(i18n("&Other...")) == 0 ) {
00608     // display "open with" dialog
00609     KOpenWithDlg* dlg = new KOpenWithDlg(list);
00610     if (dlg->exec())
00611       KRun::run(*dlg->service(), list);
00612     return;
00613   }
00614   QString qry = QString("((Type == 'Application') and (Name == '%1'))").arg( appname->latin1() );
00615   KMimeType::Ptr mime = KMimeType::findByURL( m_viewManager->activeView()->getDoc()->url() );
00616   KTrader::OfferList offers = KTrader::self()->query(mime->name(), qry);
00617   KService::Ptr app = offers.first();
00618   // some checking here: pop a wacko message it the app wasn't found.
00619   KRun::run(*app, list);
00620 }
00621 
00622 void KateMainWindow::pluginHelp()
00623 {
00624   kapp->invokeHelp (QString::null, "kate-plugins");
00625 }
00626 
00627 void KateMainWindow::setupScripts()
00628 {
00629   // locate all scripts, local as well as global.
00630   // The script manager will do the nessecary sanity checking
00631   QStringList scripts = KGlobal::dirs()->findAllResources("data", QString(kapp->name())+"/scripts/*.desktop", false, true );
00632   for (QStringList::Iterator it = scripts.begin(); it != scripts.end(); ++it )
00633     kscript->addScript( *it );
00634   QStringList l ( kscript->scripts() );
00635   for (QStringList::Iterator it=l.begin(); it != l.end(); ++it )
00636     scriptMenu->popupMenu()->insertItem( *it );
00637 }
00638 
00639 void KateMainWindow::runScript( int mIId )
00640 {
00641     //kdDebug(13001) << "Starting script engine..." << endl;
00642 //         kdDebug(13001)<<"runScript( "<<mIId<<" ) ["<<scriptMenu->popupMenu()->text( mIId )<<"]"<<endl;
00643     kscript->runScript( scriptMenu->popupMenu()->text( mIId ) );
00644 }
00645 
00646 void KateMainWindow::slotMail()
00647 {
00648   KateMailDialog *d = new KateMailDialog(this, this);
00649   if ( ! d->exec() )
00650     return;
00651   QPtrList<Kate::Document> attDocs = d->selectedDocs();
00652   delete d;
00653   // Check that all selected files are saved (or shouldn't be)
00654   QStringList urls; // to atthatch
00655   Kate::Document *doc;
00656   QPtrListIterator<Kate::Document> it(attDocs);
00657   for ( ; it.current(); ++it ) {
00658     doc = it.current();
00659     if (!doc) continue;
00660     if ( doc->url().isEmpty() ) {
00661       // unsaved document. back out unless it gets saved
00662       int r = KMessageBox::questionYesNo( this,
00663               i18n("<p>The current document has not been saved, and "
00664               "cannot be attached to an email message."
00665               "<p>Do you want to save it and proceed?"),
00666               i18n("Cannot Send Unsaved File") );
00667       if ( r == KMessageBox::Yes ) {
00668         Kate::View *v = (Kate::View*)doc->views().first();
00669         int sr = v->saveAs();
00670         if ( sr == Kate::View::SAVE_OK ) { ;
00671         }
00672         else {
00673           if ( sr != Kate::View::SAVE_CANCEL ) // ERROR or RETRY(?)
00674             KMessageBox::sorry( this, i18n("The file could not be saved. Please check "
00675                                         "if you have write permission.") );
00676           continue;
00677         }
00678       }
00679       else
00680         continue;
00681     }
00682     if ( doc->isModified() ) {
00683       // warn that document is modified and offer to save it before proceeding.
00684       int r = KMessageBox::warningYesNoCancel( this,
00685                 i18n("<p>The current file:<br><strong>%1</strong><br>has been "
00686                 "modified. Modifications will not be available in the attachment."
00687                 "<p>Do you want to save it before sending it?").arg(doc->url().prettyURL()),
00688                 i18n("Save Before Sending?") );
00689       switch ( r ) {
00690         case KMessageBox::Cancel:
00691           continue;
00692         case KMessageBox::Yes:
00693           doc->save();
00694           if ( doc->isModified() ) { // read-only docs ends here, if modified. Hmm.
00695             KMessageBox::sorry( this, i18n("The file could not be saved. Please check "
00696                                       "if you have write permission.") );
00697             continue;
00698           }
00699           break;
00700         default:
00701           break;
00702       }
00703     }
00704     // finally call the mailer
00705     urls << doc->url().url();
00706   } // check selected docs done
00707   if ( ! urls.count() )
00708     return;
00709   kapp->invokeMailer( QString::null, // to
00710                       QString::null, // cc
00711                       QString::null, // bcc
00712                       QString::null, // subject
00713                       QString::null, // body
00714                       QString::null, // msgfile
00715                       urls           // urls to atthatch
00716                       );
00717 }
00718 void KateMainWindow::tipOfTheDay()
00719 {
00720   KTipDialog::showTip( /*0*/this, QString::null, true );
00721 }
00722 
00723 void KateMainWindow::slotFullScreen(bool t)
00724 {
00725   if (t)
00726     showFullScreen();
00727   else
00728     showNormal();
00729 }
00730 
00731 bool KateMainWindow::eventFilter( QObject *o, QEvent *e )
00732 {
00733 
00734   if ( o == greptool && e->type() == QEvent::Show && m_viewManager->activeView() )
00735   {
00736     if ( m_viewManager->activeView()->getDoc()->url().isLocalFile() )
00737     {
00738       greptool->updateDirName( m_viewManager->activeView()->getDoc()->url().directory() );
00739       return true;
00740     }
00741   }
00742   if ( ( o == greptool || o == console ) &&
00743       e->type() == QEvent::Hide && m_viewManager->activeView() )
00744   {
00745      m_viewManager->activeView()->setFocus();
00746      return true;
00747   }
00748 
00749   return KMDI::MainWindow::eventFilter( o, e );
00750 }
00751 
00752 bool KateMainWindow::event( QEvent *e )
00753 {
00754   uint type = e->type();
00755   if ( type == QEvent::WindowActivate && modNotification )
00756   {
00757     if ( m_modignore )
00758     {
00759       m_modignore = false;
00760       return KMDI::MainWindow::event( e );
00761     }
00762     showModOnDiskPrompt();
00763   }
00764   // Try to disable the modonhd prompt from showing after internal dialogs.
00765   // TODO make this work better.
00766   else if ( (type == QEvent::WindowUnblocked || type == QEvent::WindowBlocked) && modNotification)
00767     m_modignore = true;
00768 
00769   return KMDI::MainWindow::event( e );
00770 }
00771 
00772 bool KateMainWindow::showModOnDiskPrompt()
00773 {
00774   Kate::Document *doc;
00775 
00776   DocVector list( KateDocManager::self()->documents() );
00777   uint cnt = 0;
00778   for( doc = KateDocManager::self()->firstDocument(); doc; doc = KateDocManager::self()->nextDocument() )
00779   {
00780     if ( KateDocManager::self()->documentInfo( doc )->modifiedOnDisc )
00781     {
00782       list.insert( cnt, doc );
00783       cnt++;
00784     }
00785   }
00786 
00787   if ( cnt )
00788   {
00789     list.resize( cnt );
00790     KateMwModOnHdDialog mhdlg( list, this );
00791     bool res = mhdlg.exec();
00792 
00793     return res;
00794   }
00795   return true;
00796 }
00797 
00798 KMDI::ToolViewAccessor *KateMainWindow::addToolView(KDockWidget::DockPosition position, QWidget *widget, const QPixmap &icon, const QString &sname, const QString &tabToolTip, const QString &tabCaption)
00799 {
00800   widget->setIcon(icon);
00801   widget->setCaption(sname);
00802 
00803   return addToolWindow(widget, position, getMainDockWidget(), 25, tabToolTip, tabCaption);
00804 }
00805 
00806 bool KateMainWindow::removeToolView(QWidget *w)
00807 {
00808   deleteToolWindow (w);
00809   return true;
00810 }
00811 
00812 bool KateMainWindow::removeToolView(KMDI::ToolViewAccessor *accessor)
00813 {
00814   deleteToolWindow (accessor);
00815   return true;
00816 }
00817 
00818 bool KateMainWindow::showToolView(QWidget *){return false;}
00819 bool KateMainWindow::showToolView(KMDI::ToolViewAccessor *){return false;}
00820 
00821 bool KateMainWindow::hideToolView(QWidget *){return false;}
00822 bool KateMainWindow::hideToolView(KMDI::ToolViewAccessor *){return false;}
00823 
00824 void KateMainWindow::slotProjectNew ()
00825 {
00826   ProjectInfo *info = KateProjectManager::self()->newProjectDialog (this);
00827 
00828   if (info)
00829   {
00830     createProject (info->type, info->name, info->fileName);
00831     delete info;
00832   }
00833 }
00834 
00835 void KateMainWindow::slotProjectOpen ()
00836 {
00837   QString fileName = KFileDialog::getOpenFileName (QString::null, QString ("*.kateproject|") + i18n("Kate Project Files") + QString (" (*.kateproject)"), this, i18n("Open Kate Project"));
00838 
00839   if (!fileName.isEmpty())
00840     openProject (fileName);
00841 }
00842 
00843 void KateMainWindow::slotProjectSave ()
00844 {
00845   if (m_project)
00846     m_project->save ();
00847 }
00848 
00849 void KateMainWindow::slotProjectClose ()
00850 {
00851   if (m_project)
00852   {
00853     KateProjectManager::self()->close (m_project);
00854   }
00855 }
00856 
00857 void KateMainWindow::activateProject (Kate::Project *project)
00858 {
00859 //   kdDebug(13001)<<"activating project "<<project<<endl;
00860   if (m_project)
00861     KateProjectManager::self()->disableProjectGUI (m_project, this);
00862 
00863   if (project)
00864     KateProjectManager::self()->enableProjectGUI (project, this);
00865 
00866   m_project = project;
00867 
00868   if (project)
00869   {
00870     KateProjectManager::self()->setCurrentProject (project);
00871     m_projectNumber = project->projectNumber ();
00872   }
00873   else
00874     m_projectNumber = 0;
00875 
00876   saveProject->setEnabled(project != 0);
00877   closeProject->setEnabled(project != 0);
00878 
00879   emit m_mainWindow->projectChanged ();
00880 }
00881 
00882 Kate::Project *KateMainWindow::createProject (const QString &type, const QString &name, const QString &filename)
00883 {
00884   Kate::Project *project = KateProjectManager::self()->create (type, name, filename);
00885 
00886   if (project)
00887     activateProject (project);
00888 
00889   return project;
00890 }
00891 
00892 Kate::Project *KateMainWindow::openProject (const QString &filename)
00893 {
00894   Kate::Project *project = KateProjectManager::self()->open (filename);
00895 
00896   if (project)
00897   {
00898     recentProjects->addURL ( KURL(filename) );
00899     activateProject (project);
00900   }
00901 
00902   return project;
00903 }
00904 
00905 void KateMainWindow::projectDeleted (uint projectNumber)
00906 {
00907   if (projectNumber == m_projectNumber)
00908   {
00909     if (KateProjectManager::self()->projects() > 0)
00910       activateProject (KateProjectManager::self()->project(KateProjectManager::self()->projects()-1));
00911     else
00912       activateProject (0);
00913   }
00914 }
00915 
00916 void KateMainWindow::slotDocumentCreated (Kate::Document *doc)
00917 {
00918   connect(doc,SIGNAL(modStateChanged(Kate::Document *)),this,SLOT(updateCaption(Kate::Document *)));
00919   connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(updateCaption(Kate::Document *)));
00920   connect(doc,SIGNAL(nameChanged(Kate::Document *)),this,SLOT(slotUpdateOpenWith()));
00921 
00922   updateCaption (doc);
00923 }
00924 
00925 void KateMainWindow::updateCaption (Kate::Document *doc)
00926 {
00927   if (!m_viewManager->activeView())
00928   {
00929     setCaption ("", false);
00930     return;
00931   }
00932 
00933   if (!(m_viewManager->activeView()->getDoc() == doc))
00934     return;
00935 
00936   QString c;
00937   if (m_viewManager->activeView()->getDoc()->url().isEmpty() || (!m_viewManager->getShowFullPath()))
00938   {
00939     c = m_viewManager->activeView()->getDoc()->docName();
00940   }
00941   else
00942   {
00943     c = m_viewManager->activeView()->getDoc()->url().prettyURL();
00944   }
00945 
00946   setCaption( KStringHandler::lsqueeze(c,64), m_viewManager->activeView()->getDoc()->isModified());
00947 }
00948 
00949 void KateMainWindow::openConstURLProject (const KURL&url)
00950 {
00951   openProject (url.path());
00952 }
00953 
00954 void KateMainWindow::saveProperties(KConfig *config) {
00955   kdDebug(13000)<<"KateMainWindow::saveProperties(): group: "<<config->group()<<endl;
00956   assert(config);
00957 
00958 //   kdDebug(13000)<<"preparing session saving"<<endl;
00959   QString grp=config->group();
00960   QString dockGrp;
00961 
00962   if (kapp->sessionSaving()) dockGrp=grp+"-Docking";
00963     else dockGrp="MainWindow0-Docking";
00964 /*  if (config->readNumEntry("GUIMode",KMdi::UndefinedMode)!=mdiMode()) {
00965         config->writeEntry("GUIMode",mdiMode());
00966         config->deleteGroup("MainWindow0-Docking");
00967   }*/
00968 
00969 //   kdDebug(13000)<<"Before write dock config"<<endl;
00970   writeDockConfig(config,dockGrp);
00971 //   kdDebug(13000)<<"After write dock config"<<endl;
00972 
00973 
00974   if (kapp->sessionSaving()) dockGrp=grp+"-View Configuration";
00975     else dockGrp="MainWindow0-View Configuration";
00976 
00977   m_viewManager->saveViewConfiguration (config,dockGrp);
00978 //   kdDebug(13000)<<"After saving view configuration"<<endl;
00979   config->setGroup(grp);
00980 
00981 }
00982 
00983 void KateMainWindow::readProperties(KConfig *config)
00984 {
00985   QString grp=config->group();
00986   QString dockGrp;
00987 
00988   if (kapp->isRestored()) dockGrp=grp+"-Docking";
00989     else dockGrp="MainWindow0-Docking";
00990 
00991   if (config->hasGroup(dockGrp))
00992         readDockConfig(config,dockGrp);
00993 
00994   if (kapp->isRestored()) dockGrp=grp+"-View Configuration";
00995     else dockGrp="MainWindow0-View Configuration";
00996 
00997   m_viewManager->restoreViewConfiguration (config,dockGrp);
00998   config->setGroup(grp);
00999 }
01000 
01001 void KateMainWindow::saveGlobalProperties( KConfig* sessionConfig )
01002 {
01003   KateProjectManager::self()->saveProjectList (sessionConfig);
01004   KateDocManager::self()->saveDocumentList (sessionConfig);
01005 }
01006 
01007 void KateMainWindow::slotPipeToConsole ()
01008 {
01009   if (!console)
01010     return;
01011 
01012   Kate::View *v = m_viewManager->activeView();
01013 
01014   if (!v)
01015     return;
01016 
01017   if (v->getDoc()->hasSelection ())
01018     console->sendInput (v->getDoc()->selection());
01019   else
01020     console->sendInput (v->getDoc()->text());
01021 }
01022 // 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.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Nov 4 00:48:31 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003