addapplicationdlg.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "addapplicationdlg.h"
00013
00014 #include <qcheckbox.h>
00015 #include <qcombobox.h>
00016 #include <qfile.h>
00017 #include <qheader.h>
00018 #include <qlineedit.h>
00019 #include <qlistview.h>
00020 #include <qtextstream.h>
00021 #include <qapplication.h>
00022 #include <kdebug.h>
00023 #include <kicondialog.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <kmimetype.h>
00027
00028 #include "autolistviewitems.h"
00029
00030 #include "misc.h"
00031 #include "autoprojectwidget.h"
00032 #include "autoprojectpart.h"
00033
00034
00035 AddApplicationDialog::AddApplicationDialog(AutoProjectWidget *widget, SubprojectItem *spitem,
00036 QWidget *parent, const char *name)
00037 : AddApplicationDialogBase(parent, name, true)
00038 {
00039 filename_edit->setText(".desktop");
00040 filename_edit->home(false);
00041 filename_edit->setFocus();
00042 chosentypes_listview->header()->hide();
00043 availtypes_listview->header()->hide();
00044 QString addApplication = add_button->text();
00045 QString removeApplication = remove_button->text();
00046
00047 add_button->setText( QApplication::reverseLayout() ? removeApplication : addApplication );
00048 remove_button->setText( QApplication::reverseLayout() ? addApplication : removeApplication );
00049
00050 m_widget = widget;
00051 subProject = spitem;
00052
00053
00054 QPtrListIterator<TargetItem> tit(spitem->targets);
00055 for (; tit.current(); ++tit) {
00056 if ((*tit)->primary == "PROGRAMS")
00057 executable_combo->insertItem(QString((*tit)->name));
00058 }
00059
00060
00061 KMimeType::List l = KMimeType::allMimeTypes();
00062 KMimeType::List::Iterator it;
00063 for (it = l.begin(); it != l.end(); ++it)
00064 new QListViewItem(availtypes_listview, (*it)->name());
00065
00066 setIcon ( SmallIcon ( "window_new" ) );
00067 }
00068
00069
00070 AddApplicationDialog::~AddApplicationDialog()
00071 {}
00072
00073
00074 void AddApplicationDialog::iconClicked()
00075 {
00076 KIconLoader *loader = AutoProjectFactory::instance()->iconLoader();
00077 KIconDialog dlg(loader, this);
00078 QString name = dlg.selectIcon(KIcon::Desktop);
00079 if (!name.isNull()) {
00080 iconName = name;
00081 icon_button->setPixmap(loader->loadIcon(name, KIcon::Desktop));
00082 }
00083 }
00084
00085
00086 void AddApplicationDialog::addTypeClicked()
00087 {
00088 QListViewItem *selitem = availtypes_listview->selectedItem();
00089 if (!selitem)
00090 return;
00091
00092 QListViewItem *olditem = chosentypes_listview->firstChild();
00093 while (olditem) {
00094 if (selitem->text(0) == olditem->text(0))
00095 return;
00096 olditem = olditem->nextSibling();
00097 }
00098 new QListViewItem(chosentypes_listview, selitem->text(0));
00099 }
00100
00101
00102 void AddApplicationDialog::removeTypeClicked()
00103 {
00104 delete chosentypes_listview->currentItem();
00105 }
00106
00107
00108 void AddApplicationDialog::accept()
00109 {
00110
00111 QStringList mimeTypes;
00112 QListViewItem *item = chosentypes_listview->firstChild();
00113 while (item) {
00114 mimeTypes.append(item->text(0));
00115 item = item->nextSibling();
00116 }
00117
00118
00119 QString fileName = filename_edit->text();
00120 if (fileName.isEmpty() || fileName == ".desktop") {
00121 KMessageBox::sorry(this, i18n("You have to enter a file name."));
00122 filename_edit->setFocus();
00123 return;
00124 }
00125
00126 QString executable = executable_combo->currentText();
00127 if (executable.isEmpty()) {
00128 KMessageBox::sorry(this, i18n("You have to enter the file name of an executable program."));
00129 executable_combo->setFocus();
00130 return;
00131 }
00132
00133 QString name = name_edit->text();
00134 if (name.isEmpty()) {
00135 KMessageBox::sorry(this, i18n("You have to enter an application name."));
00136 name_edit->setFocus();
00137 return;
00138 }
00139
00140 QFile f(subProject->path + "/" + fileName);
00141 if (f.exists()) {
00142 KMessageBox::sorry(this, i18n("A file with this name exists already."));
00143 filename_edit->setFocus();
00144 return;
00145 }
00146 if (!f.open(IO_WriteOnly)) {
00147 KMessageBox::sorry(this, i18n("Could not open file for writing."));
00148 return;
00149 }
00150
00151 QTextStream stream(&f);
00152 stream << "[Desktop Entry]" << endl;
00153 stream << "Type=Application" << endl;
00154 stream << "Name=" << name << endl;
00155 stream << "Exec=" << (executable + " -caption \"%c\" %i %m %u") << endl;
00156 stream << "Comment=" << comment_edit->text() << endl;
00157 if (!iconName.isNull())
00158 stream << "Icon=" << iconName << endl;
00159 stream << "MimeTypes=" << mimeTypes.join(";") << endl;
00160 stream << "Terminal=" << (terminal_box->isChecked()? "true" : "false") << endl;
00161 f.close();
00162
00163
00164
00165 QString section = section_combo->currentText();
00166 QString appsdir = "$(kde_appsdir)/" + section;
00167 QMap<QString,QString>::ConstIterator it;
00168 for (it = subProject->prefixes.begin(); it != subProject->prefixes.end(); ++it)
00169 if (it.data() == appsdir)
00170 break;
00171
00172 QMap<QString, QString> replaceMap;
00173 QString prefix;
00174 if (it == subProject->prefixes.end()) {
00175 prefix = "applnk" + section;
00176 replaceMap.insert(prefix + "dir", appsdir);
00177 subProject->prefixes.insert(prefix, appsdir);
00178 } else {
00179 prefix = it.key();
00180 }
00181 QString varname = prefix + "_DATA";
00182
00183
00184
00185 TargetItem *titem = 0;
00186 for (uint i=0; i < subProject->targets.count(); ++i) {
00187 TargetItem *tmptitem = subProject->targets.at(i);
00188 if ("DATA" == tmptitem->primary && prefix == tmptitem->prefix) {
00189 titem = tmptitem;
00190 break;
00191 }
00192 }
00193 if (!titem) {
00194 titem = m_widget->createTargetItem("", prefix, "DATA", false);
00195 subProject->targets.append(titem);
00196 }
00197
00198 FileItem *fitem = m_widget->createFileItem(fileName, subProject);
00199 titem->sources.append(fitem);
00200
00201 subProject->variables[varname] += (" " + fileName);
00202 replaceMap.insert(varname, subProject->variables[varname]);
00203 AutoProjectTool::modifyMakefileam(subProject->path + "/Makefile.am", replaceMap);
00204
00205 QDialog::accept();
00206 }
00207
00208 #include "addapplicationdlg.moc"
This file is part of the documentation for KDevelop Version 3.1.2.