KDevelop API Documentation

buildtools/autotools/addservicedlg.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2001 by Bernd Gehrmann * 00003 * bernd@kdevelop.org * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include "addservicedlg.h" 00013 00014 #include <qcombobox.h> 00015 #include <qfile.h> 00016 #include <qheader.h> 00017 #include <qlineedit.h> 00018 #include <qlistview.h> 00019 #include <qtextstream.h> 00020 #include <kdebug.h> 00021 #include <kicondialog.h> 00022 #include <klineeditdlg.h> 00023 #include <klocale.h> 00024 #include <kmessagebox.h> 00025 #include <kservicetype.h> 00026 00027 #include "autolistviewitems.h" 00028 00029 #include "misc.h" 00030 #include "autoprojectwidget.h" 00031 #include "autoprojectpart.h" 00032 00033 00034 AddServiceDialog::AddServiceDialog(AutoProjectWidget *widget, SubprojectItem *spitem, 00035 QWidget *parent, const char *name) 00036 : AddServiceDialogBase(parent, name, true) 00037 { 00038 filename_edit->setText(".desktop"); 00039 filename_edit->home(false); 00040 filename_edit->setFocus(); 00041 chosentypes_listview->header()->hide(); 00042 availtypes_listview->header()->hide(); 00043 00044 m_widget = widget; 00045 subProject = spitem; 00046 00047 // Fill the combo box with library names in the directory 00048 QPtrListIterator<TargetItem> tit(spitem->targets); 00049 for (; tit.current(); ++tit) { 00050 if ((*tit)->primary == "LTLIBRARIES") 00051 library_combo->insertItem(QString((*tit)->name)); 00052 } 00053 00054 // Fill the list of available service types 00055 KServiceType::List l = KServiceType::allServiceTypes(); 00056 KServiceType::List::Iterator it; 00057 for (it = l.begin(); it != l.end(); ++it) 00058 if (!(*it)->isType(KST_KMimeType)) 00059 new QListViewItem(availtypes_listview, (*it)->name()); 00060 00061 setIcon ( SmallIcon ( "servicenew_kdevelop.png" ) ); 00062 } 00063 00064 00065 AddServiceDialog::~AddServiceDialog() 00066 {} 00067 00068 00069 void AddServiceDialog::updateProperties() 00070 { 00071 QStringList props; 00072 00073 QListViewItem *item = static_cast<QCheckListItem*>(chosentypes_listview->firstChild()); 00074 while (item) { 00075 KServiceType::Ptr type = KServiceType::serviceType(item->text(0)); 00076 if (type) { 00077 QStringList stprops = type->propertyDefNames(); 00078 QStringList::ConstIterator stit; 00079 for (stit = stprops.begin(); stit != stprops.end(); ++stit) 00080 if (props.find(*stit) == props.end() && (*stit) != "Name" && (*stit) != "Comment" 00081 && (*stit) != "Icon") 00082 props.append(*stit); 00083 } 00084 item = item->nextSibling(); 00085 } 00086 00087 properties_listview->clear(); 00088 QStringList::ConstIterator it; 00089 for (it = props.begin(); it != props.end(); ++it) 00090 new QListViewItem(properties_listview, *it); 00091 } 00092 00093 00094 void AddServiceDialog::iconClicked() 00095 { 00096 KIconLoader *loader = AutoProjectFactory::instance()->iconLoader(); 00097 KIconDialog dlg(loader, this); 00098 QString name = dlg.selectIcon(KIcon::Desktop); 00099 if (!name.isNull()) { 00100 iconName = name; 00101 icon_button->setPixmap(loader->loadIcon(name, KIcon::Desktop)); 00102 } 00103 } 00104 00105 00106 void AddServiceDialog::addTypeClicked() 00107 { 00108 QListViewItem *selitem = availtypes_listview->selectedItem(); 00109 if (!selitem) 00110 return; 00111 00112 QListViewItem *olditem = chosentypes_listview->firstChild(); 00113 while (olditem) { 00114 if (selitem->text(0) == olditem->text(0)) 00115 return; 00116 olditem = olditem->nextSibling(); 00117 } 00118 new QListViewItem(chosentypes_listview, selitem->text(0)); 00119 00120 updateProperties(); 00121 } 00122 00123 00124 void AddServiceDialog::removeTypeClicked() 00125 { 00126 delete chosentypes_listview->currentItem(); 00127 00128 updateProperties(); 00129 } 00130 00131 00132 void AddServiceDialog::propertyExecuted(QListViewItem *item) 00133 { 00134 if (!item) 00135 return; 00136 00137 QString prop = item->text(0); 00138 QString value = item->text(1); 00139 bool ok; 00140 value = KLineEditDlg::getText(i18n("Property %1:").arg(prop), value, &ok, this); 00141 if (!ok) 00142 return; 00143 00144 item->setText(1, value); 00145 } 00146 00147 00148 void AddServiceDialog::accept() 00149 { 00150 // Create list of service types 00151 QStringList serviceTypes; 00152 QListViewItem *item = chosentypes_listview->firstChild(); 00153 while (item) { 00154 serviceTypes.append(item->text(0)); 00155 item = item->nextSibling(); 00156 } 00157 00158 // Some plausibility tests 00159 QString fileName = filename_edit->text(); 00160 if (fileName.isEmpty() || fileName == ".desktop") { 00161 KMessageBox::sorry(this, i18n("You have to enter a file name.")); 00162 filename_edit->setFocus(); 00163 return; 00164 } 00165 00166 QString name = name_edit->text(); 00167 if (name.isEmpty()) { 00168 KMessageBox::sorry(this, i18n("You have to enter a service name.")); 00169 name_edit->setFocus(); 00170 return; 00171 } 00172 00173 QFile f(subProject->path + "/" + fileName); 00174 if (f.exists()) { 00175 KMessageBox::sorry(this, i18n("A file with this name exists already.")); 00176 filename_edit->setFocus(); 00177 return; 00178 } 00179 if (!f.open(IO_WriteOnly)) { 00180 KMessageBox::sorry(this, i18n("Could not open file for writing.")); 00181 return; 00182 } 00183 00184 QTextStream stream(&f); 00185 stream << "[Desktop Entry]" << endl; 00186 stream << "Type=Service" << endl; 00187 stream << "Name=" << name << endl; 00188 stream << "Comment=" << comment_edit->text() << endl; 00189 if (!iconName.isNull()) 00190 stream << "Icon=" << iconName << endl; 00191 stream << "ServiceTypes=" << serviceTypes.join(",") << endl; 00192 item = properties_listview->firstChild(); 00193 while (item) { 00194 stream << item->text(0) << "=" << item->text(1) << endl; 00195 item = item->nextSibling(); 00196 } 00197 f.close(); 00198 00199 // Find a prefix that points to the services directory. 00200 // If there is none, use kde_services 00201 QMap<QString,QString>::ConstIterator it; 00202 for (it = subProject->prefixes.begin(); it != subProject->prefixes.end(); ++it) 00203 if (it.data() == "$(kde_servicesdir)") 00204 break; 00205 QString prefix = (it == subProject->prefixes.end())? QString("kde_services") : it.key(); 00206 QString varname = prefix + "_DATA"; 00207 00208 // Look if a list view item for this prefix exists already. 00209 // Create a new one otherwise 00210 TargetItem *titem = 0; 00211 for (uint i=0; i < subProject->targets.count(); ++i) { 00212 TargetItem *tmptitem = subProject->targets.at(i); 00213 if ("DATA" == tmptitem->primary && prefix == tmptitem->prefix) { 00214 titem = tmptitem; 00215 break; 00216 } 00217 } 00218 if (!titem) { 00219 titem = m_widget->createTargetItem("", prefix, "DATA", false); 00220 subProject->targets.append(titem); 00221 } 00222 // Add this file to the target 00223 FileItem *fitem = m_widget->createFileItem(fileName, subProject); 00224 titem->sources.append(fitem); 00225 00226 subProject->variables[varname] += (" " + fileName); 00227 QMap<QString, QString> replaceMap; 00228 replaceMap.insert(varname, subProject->variables[varname]); 00229 AutoProjectTool::modifyMakefileam(subProject->path + "/Makefile.am", replaceMap); 00230 00231 QDialog::accept(); 00232 } 00233 00234 #include "addservicedlg.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Oct 19 08:01:35 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003