kateprojectmanager.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 KateProjectManager *KateProjectManager::self ()
00057 {
00058 return KateApp::self()->kateProjectManager ();
00059 }
00060
00061 void KateProjectManager::setupPluginList ()
00062 {
00063 QValueList<KService::Ptr> traderList= KTrader::self()->query("Kate/ProjectPlugin");
00064
00065 KTrader::OfferList::Iterator it(traderList.begin());
00066 for( ; it != traderList.end(); ++it)
00067 {
00068 KService::Ptr ptr = (*it);
00069
00070 QString pVersion = ptr->property("X-Kate-Version").toString();
00071
00072 if ((pVersion >= "2.2") && (pVersion <= KATE_VERSION))
00073 {
00074 ProjectPluginInfo *info=new ProjectPluginInfo;
00075
00076 info->service = ptr;
00077 info->projectType=info->service->property("X-Kate-ProjectType").toString();
00078
00079 m_pluginList.append(info);
00080 }
00081 }
00082 }
00083
00084 void KateProjectManager::setCurrentProject (Kate::Project *project)
00085 {
00086 m_currentProject = project;
00087
00088 emit m_projectManager->projectChanged ();
00089 }
00090
00091 Kate::Project *KateProjectManager::create (const QString &type, const QString &name, const QString &filename)
00092 {
00093 KConfig *c = new KConfig (filename, false, false);
00094
00095 c->setGroup("Project File");
00096 c->writeEntry ("Type", type);
00097 c->writeEntry ("Name", name);
00098 c->sync ();
00099
00100 delete c;
00101
00102 return open (filename);
00103 }
00104
00105 Kate::Project *KateProjectManager::open (const QString &filename)
00106 {
00107
00108 for (uint z=0; z < m_projects.count(); z++)
00109 if (m_projects.at(z)->fileName() == filename)
00110 return 0;
00111
00112 KateInternalProjectData *data = new KateInternalProjectData ();
00113 data->proMan = this;
00114 data->fileName = filename;
00115
00116 Kate::Project *project = new Kate::Project ((void *) data);
00117
00118 m_projects.append (project);
00119
00120 emit m_projectManager->projectCreated (project);
00121
00122 return project;
00123 }
00124
00125 bool KateProjectManager::close (Kate::Project *project, bool force)
00126 {
00127 if (project)
00128 {
00129 if (project->close() || force)
00130 {
00131 uint id = project->projectNumber ();
00132 int n = m_projects.findRef (project);
00133
00134 if (n >= 0)
00135 {
00136 if (Kate::pluginViewInterface(project->plugin()))
00137 {
00138 for (uint i=0; i< ((KateApp*)parent())->mainWindows(); i++)
00139 {
00140 Kate::pluginViewInterface(project->plugin())->removeView(((KateApp*)parent())->mainWindow(i));
00141 }
00142 }
00143
00144 m_projects.remove (n);
00145
00146 emit m_projectManager->projectDeleted (id);
00147
00148 return true;
00149 }
00150 }
00151 }
00152
00153 return false;
00154 }
00155
00156 Kate::Project *KateProjectManager::project (uint n)
00157 {
00158 if (n >= m_projects.count())
00159 return 0;
00160
00161 return m_projects.at(n);
00162 }
00163
00164 uint KateProjectManager::projects ()
00165 {
00166 return m_projects.count ();
00167 }
00168
00169 Kate::ProjectPlugin *KateProjectManager::createPlugin (Kate::Project *project)
00170 {
00171 ProjectPluginInfo *def = 0;
00172 ProjectPluginInfo *info = 0;
00173
00174 for (uint i=0; i<m_pluginList.count(); i++)
00175 {
00176 if (m_pluginList.at(i)->projectType == project->type())
00177 {
00178 info = m_pluginList.at(i);
00179 break;
00180 }
00181 else if (m_pluginList.at(i)->projectType == QString ("Default"))
00182 def = m_pluginList.at(i);
00183 }
00184
00185 if (!info)
00186 info = def;
00187
00188 return Kate::createProjectPlugin (QFile::encodeName(info->service->library()), project);
00189 }
00190
00191 void KateProjectManager::enableProjectGUI (Kate::Project *project, KateMainWindow *win)
00192 {
00193 if (!project->plugin()) return;
00194 if (!Kate::pluginViewInterface(project->plugin())) return;
00195
00196 Kate::pluginViewInterface(project->plugin())->addView(win->mainWindow());
00197 }
00198
00199 void KateProjectManager::disableProjectGUI (Kate::Project *project, KateMainWindow *win)
00200 {
00201 if (!project->plugin()) return;
00202 if (!Kate::pluginViewInterface(project->plugin())) return;
00203
00204 Kate::pluginViewInterface(project->plugin())->removeView(win->mainWindow());
00205 }
00206
00207 ProjectInfo *KateProjectManager::newProjectDialog (QWidget *parent)
00208 {
00209 ProjectInfo *info = 0;
00210
00211 KateProjectDialogNew* dlg = new KateProjectDialogNew (parent);
00212
00213 int n = dlg->exec();
00214
00215 if (n)
00216 {
00217 info = new ProjectInfo ();
00218 info->type = dlg->type;
00219 info->name = dlg->name;
00220 info->fileName = dlg->fileName;
00221 }
00222
00223 delete dlg;
00224 return info;
00225 }
00226
00227 QStringList KateProjectManager::pluginStringList ()
00228 {
00229 QStringList list;
00230
00231 for (uint i=0; i<m_pluginList.count(); i++)
00232 list.push_back (m_pluginList.at(i)->projectType);
00233
00234 return list;
00235 }
00236
00237 bool KateProjectManager::queryCloseAll ()
00238 {
00239 for (uint z=0; z < m_projects.count(); z++)
00240 if (!m_projects.at(z)->queryClose())
00241 return false;
00242
00243 return true;
00244 }
00245
00246 bool KateProjectManager::closeAll ()
00247 {
00248 while (!m_projects.isEmpty())
00249 {
00250 if (!close(m_projects.at(m_projects.count()-1)))
00251 return false;
00252 }
00253
00254 return true;
00255 }
00256
00257 void KateProjectManager::saveProjectList (class KConfig *config)
00258 {
00259 QString prevGrp=config->group();
00260 config->setGroup ("Open Projects");
00261
00262 config->writeEntry ("Count", m_projects.count());
00263
00264 for (uint z=0; z < m_projects.count(); z++)
00265 config->writeEntry( QString("Project %1").arg(z), m_projects.at(z)->fileName() );
00266
00267 config->setGroup(prevGrp);
00268 }
00269
00270 void KateProjectManager::restoreProjectList (class KConfig *config)
00271 {
00272 config->setGroup ("Open Projects");
00273
00274 int count = config->readNumEntry("Count");
00275
00276 int i = 0;
00277 while ((i < count) && config->hasKey(QString("Project %1").arg(i)))
00278 {
00279 QString fn = config->readEntry( QString("Project %1").arg( i ) );
00280
00281 if ( !fn.isEmpty() )
00282 open (fn);
00283
00284 i++;
00285 }
00286 }
00287
00288
00289
00290
00291
00292 KateProjectDialogNew::KateProjectDialogNew (QWidget *parent)
00293 : KDialogBase (parent, "project_new", true, i18n ("New Project"), KDialogBase::Ok|KDialogBase::Cancel)
00294 {
00295 QWidget *page = new QWidget( this );
00296 setMainWidget(page);
00297
00298 QGridLayout *grid = new QGridLayout (page, 3, 2, 0, spacingHint());
00299
00300 grid->addWidget (new QLabel (i18n("Project type:"), page), 0, 0);
00301 m_typeCombo = new KComboBox (page);
00302 grid->addWidget (m_typeCombo, 0, 1);
00303
00304 m_typeCombo->insertStringList (KateProjectManager::self()->pluginStringList ());
00305
00306 grid->addWidget (new QLabel (i18n("Project name:"), page), 1, 0);
00307 m_nameEdit = new KLineEdit (page);
00308 grid->addWidget (m_nameEdit, 1, 1);
00309 connect( m_nameEdit, SIGNAL( textChanged ( const QString & )),this,SLOT(slotTextChanged()));
00310 grid->addWidget (new QLabel (i18n("Project file:"), page), 2, 0);
00311 m_urlRequester = new KURLRequester (page);
00312 grid->addWidget (m_urlRequester, 2, 1);
00313 m_nameEdit->setFocus();
00314
00315 m_urlRequester->setMode (KFile::LocalOnly);
00316 m_urlRequester->fileDialog()->setOperationMode (KFileDialog::Saving);
00317 m_urlRequester->setFilter (QString ("*.kateproject|")
00318 + i18n("Kate Project Files") + QString (" (*.kateproject)"));
00319 connect( m_urlRequester->lineEdit(), SIGNAL( textChanged ( const QString & )),this,SLOT(slotTextChanged()));
00320 slotTextChanged();
00321 }
00322
00323 KateProjectDialogNew::~KateProjectDialogNew ()
00324 {
00325 }
00326
00327 void KateProjectDialogNew::slotTextChanged()
00328 {
00329 enableButtonOK( !m_urlRequester->lineEdit()->text().isEmpty() && !m_nameEdit->text().isEmpty());
00330 }
00331
00332 int KateProjectDialogNew::exec()
00333 {
00334 int n = 0;
00335
00336 while ((n = KDialogBase::exec()))
00337 {
00338 type = m_typeCombo->currentText ();
00339 name = m_nameEdit->text ();
00340 fileName = m_urlRequester->url ();
00341
00342 if (!name.isEmpty() && !fileName.isEmpty())
00343 break;
00344 else
00345 KMessageBox::sorry (this, i18n ("You must enter a project name and file"));
00346 }
00347
00348 if (!fileName.endsWith (".kateproject"))
00349 fileName.append (".kateproject");
00350
00351 return n;
00352 }
This file is part of the documentation for kate Library Version 3.4.2.