kate Library API Documentation

kateprojectmanager.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Christoph Cullmann <cullmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "kateprojectmanager.h"
00020 #include "kateprojectmanager.moc"
00021 
00022 #include "kateapp.h"
00023 #include "katemainwindow.h"
00024 
00025 #include <kconfig.h>
00026 #include <kcombobox.h>
00027 #include <kdialogbase.h>
00028 #include <kurlrequester.h>
00029 #include <klineedit.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <kfiledialog.h>
00033 
00034 #include <qfile.h>
00035 #include <qlayout.h>
00036 #include <qlabel.h>
00037 
00038 KateProjectManager::KateProjectManager (QObject *parent) : QObject (parent)
00039 {
00040   m_projects.setAutoDelete (true);
00041   m_projectManager = new Kate::ProjectManager (this);
00042   setupPluginList ();
00043 }
00044 
00045 KateProjectManager::~KateProjectManager()
00046 {
00047   while (!m_projects.isEmpty())
00048   {
00049     close (m_projects.at(m_projects.count()-1), true);
00050   }
00051 
00052   m_pluginList.setAutoDelete(true);
00053   m_pluginList.clear();
00054 }
00055 
00056 void KateProjectManager::setupPluginList ()
00057 {
00058   QValueList<KService::Ptr> traderList= KTrader::self()->query("Kate/ProjectPlugin");
00059 
00060   KTrader::OfferList::Iterator it(traderList.begin());
00061   for( ; it != traderList.end(); ++it)
00062   {
00063     KService::Ptr ptr = (*it);
00064 
00065     QString pVersion = ptr->property("X-Kate-Version").toString();
00066     
00067     if ((pVersion >= "2.2") && (pVersion <= KATE_VERSION))
00068     {
00069       ProjectPluginInfo *info=new ProjectPluginInfo;
00070   
00071       info->service = ptr;
00072       info->projectType=info->service->property("X-Kate-ProjectType").toString();
00073   
00074       m_pluginList.append(info);
00075     }
00076   }
00077 }
00078 
00079 void KateProjectManager::setCurrentProject (Kate::Project *project)
00080 {
00081   m_currentProject = project;
00082 
00083   emit m_projectManager->projectChanged ();
00084 }
00085 
00086 Kate::Project *KateProjectManager::create (const QString &type, const QString &name, const QString &filename)
00087 {
00088   KConfig *c = new KConfig (filename, false, false);
00089 
00090   c->setGroup("Project File");
00091   c->writeEntry ("Type", type);
00092   c->writeEntry ("Name", name);
00093   c->sync ();
00094 
00095   delete c;
00096 
00097   return open (filename);
00098 }
00099 
00100 Kate::Project *KateProjectManager::open (const QString &filename)
00101 {
00102   // don't open a project 2 times
00103   for (uint z=0; z < m_projects.count(); z++)
00104     if (m_projects.at(z)->fileName() == filename)
00105       return 0;
00106 
00107   KateInternalProjectData *data = new KateInternalProjectData ();
00108   data->proMan = this;
00109   data->fileName = filename;
00110 
00111   Kate::Project *project = new Kate::Project ((void *) data);
00112 
00113   m_projects.append (project);
00114 
00115   emit m_projectManager->projectCreated (project);
00116 
00117   return project;
00118 }
00119 
00120 bool KateProjectManager::close (Kate::Project *project, bool force)
00121 {
00122   if (project)
00123   {
00124     if (project->close() || force)
00125     {
00126       uint id = project->projectNumber ();
00127       int n = m_projects.findRef (project);
00128 
00129       if (n >= 0)
00130       {
00131         if (Kate::pluginViewInterface(project->plugin()))
00132         {
00133           for (uint i=0; i< ((KateApp*)parent())->mainWindows(); i++)
00134           {
00135             Kate::pluginViewInterface(project->plugin())->removeView(((KateApp*)parent())->mainWindow(i));
00136           }
00137         }
00138 
00139         m_projects.remove (n);
00140 
00141         emit m_projectManager->projectDeleted (id);
00142 
00143         return true;
00144       }
00145     }
00146   }
00147 
00148   return false;
00149 }
00150 
00151 Kate::Project *KateProjectManager::project (uint n)
00152 {
00153   if (n >= m_projects.count())
00154     return 0;
00155 
00156   return m_projects.at(n);
00157 }
00158 
00159 uint KateProjectManager::projects ()
00160 {
00161   return m_projects.count ();
00162 }
00163 
00164 Kate::ProjectPlugin *KateProjectManager::createPlugin (Kate::Project *project)
00165 {
00166   ProjectPluginInfo *def = 0;
00167   ProjectPluginInfo *info = 0;
00168 
00169   for (uint i=0; i<m_pluginList.count(); i++)
00170   {
00171     if (m_pluginList.at(i)->projectType == project->type())
00172     {
00173       info = m_pluginList.at(i);
00174       break;
00175     }
00176     else if (m_pluginList.at(i)->projectType == QString ("Default"))
00177       def = m_pluginList.at(i);
00178   }
00179 
00180   if (!info)
00181     info = def;
00182 
00183   return Kate::createProjectPlugin (QFile::encodeName(info->service->library()), project);
00184 }
00185 
00186 void KateProjectManager::enableProjectGUI (Kate::Project *project, KateMainWindow *win)
00187 {
00188   if (!project->plugin()) return;
00189   if (!Kate::pluginViewInterface(project->plugin())) return;
00190 
00191   Kate::pluginViewInterface(project->plugin())->addView(win->mainWindow());
00192 }
00193 
00194 void KateProjectManager::disableProjectGUI (Kate::Project *project, KateMainWindow *win)
00195 {
00196   if (!project->plugin()) return;
00197   if (!Kate::pluginViewInterface(project->plugin())) return;
00198 
00199   Kate::pluginViewInterface(project->plugin())->removeView(win->mainWindow());
00200 }
00201 
00202 ProjectInfo *KateProjectManager::newProjectDialog (QWidget *parent)
00203 {
00204   ProjectInfo *info = 0;
00205 
00206   KateProjectDialogNew* dlg = new KateProjectDialogNew (parent, this);
00207 
00208   int n = dlg->exec();
00209 
00210   if (n)
00211   {
00212     info = new ProjectInfo ();
00213     info->type = dlg->type;
00214     info->name = dlg->name;
00215     info->fileName = dlg->fileName;
00216   }
00217 
00218   delete dlg;
00219   return info;
00220 }
00221 
00222 QStringList KateProjectManager::pluginStringList ()
00223 {
00224   QStringList list;
00225 
00226   for (uint i=0; i<m_pluginList.count(); i++)
00227     list.push_back (m_pluginList.at(i)->projectType);
00228 
00229   return list;
00230 }
00231 
00232 bool KateProjectManager::queryCloseAll ()
00233 {
00234   for (uint z=0; z < m_projects.count(); z++)
00235     if (!m_projects.at(z)->queryClose())
00236       return false;
00237 
00238   return true;
00239 }
00240 
00241 bool KateProjectManager::closeAll ()
00242 {
00243   while (!m_projects.isEmpty())
00244   {
00245     if (!close(m_projects.at(m_projects.count()-1)))
00246       return false;
00247   }
00248 
00249   return true;
00250 }
00251 
00252 void KateProjectManager::saveProjectList (class KConfig *config)
00253 {
00254   QString prevGrp=config->group();
00255   config->setGroup ("Open Projects");
00256 
00257   config->writeEntry ("Count", m_projects.count());
00258 
00259   for (uint z=0; z < m_projects.count(); z++)
00260     config->writeEntry( QString("Project %1").arg(z), m_projects.at(z)->fileName() );
00261 
00262   config->setGroup(prevGrp);
00263 }
00264 
00265 void KateProjectManager::restoreProjectList (class KConfig *config)
00266 {
00267   config->setGroup ("Open Projects");
00268 
00269   int count = config->readNumEntry("Count");
00270 
00271   int i = 0;
00272   while ((i < count) && config->hasKey(QString("Project %1").arg(i)))
00273   {
00274     QString fn = config->readEntry( QString("Project %1").arg( i ) );
00275 
00276     if ( !fn.isEmpty() )
00277       open (fn);
00278 
00279     i++;
00280   }
00281 }
00282 
00283 //
00284 // "New Project" Dialog
00285 //
00286 
00287 KateProjectDialogNew::KateProjectDialogNew (QWidget *parent, KateProjectManager *projectMan) : KDialogBase (parent, "project_new", true, i18n ("New Project"), KDialogBase::Ok|KDialogBase::Cancel)
00288 {
00289   m_projectMan = projectMan;
00290 
00291   QWidget *page = new QWidget( this );
00292   setMainWidget(page);
00293 
00294   QGridLayout *grid = new QGridLayout (page, 3, 2, 0, spacingHint());
00295 
00296   grid->addWidget (new QLabel (i18n("Project type:"), page), 0, 0);
00297   m_typeCombo = new KComboBox (page);
00298   grid->addWidget (m_typeCombo, 0, 1);
00299 
00300   m_typeCombo->insertStringList (m_projectMan->pluginStringList ());
00301 
00302   grid->addWidget (new QLabel (i18n("Project name:"), page), 1, 0);
00303   m_nameEdit = new KLineEdit (page);
00304   grid->addWidget (m_nameEdit, 1, 1);
00305   connect( m_nameEdit, SIGNAL( textChanged ( const QString & )),this,SLOT(slotTextChanged()));
00306   grid->addWidget (new QLabel (i18n("Project file:"), page), 2, 0);
00307   m_urlRequester = new KURLRequester (page);
00308   grid->addWidget (m_urlRequester, 2, 1);
00309   m_nameEdit->setFocus();
00310 
00311   m_urlRequester->setMode (KFile::LocalOnly);
00312   m_urlRequester->fileDialog()->setOperationMode (KFileDialog::Saving);
00313   m_urlRequester->setFilter (QString ("*.kateproject|")
00314                              + i18n("Kate Project Files") + QString (" (*.kateproject)"));
00315   connect( m_urlRequester->lineEdit(), SIGNAL( textChanged ( const QString & )),this,SLOT(slotTextChanged()));
00316   slotTextChanged();
00317 }
00318 
00319 KateProjectDialogNew::~KateProjectDialogNew ()
00320 {
00321 }
00322 
00323 void KateProjectDialogNew::slotTextChanged()
00324 {
00325   enableButtonOK( !m_urlRequester->lineEdit()->text().isEmpty() && !m_nameEdit->text().isEmpty());
00326 }
00327 
00328 int KateProjectDialogNew::exec()
00329 {
00330   int n = 0;
00331 
00332   while ((n = KDialogBase::exec()))
00333   {
00334     type = m_typeCombo->currentText ();
00335     name = m_nameEdit->text ();
00336     fileName = m_urlRequester->url ();
00337 
00338     if (!name.isEmpty() && !fileName.isEmpty())
00339       break;
00340     else
00341       KMessageBox::sorry (this, i18n ("You must enter a project name and file"));
00342   }
00343   
00344   if (!fileName.endsWith (".kateproject"))
00345     fileName.append (".kateproject");
00346 
00347   return n;
00348 }
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