diff options
author | william os4y <william.os4y@gmail.com> | 2015-03-05 21:17:07 +0100 |
---|---|---|
committer | william <william.os4y@gmail.com> | 2015-03-13 20:51:55 +0100 |
commit | 3f8520006b5634cdea0c29953839bff3d7169fd9 (patch) | |
tree | abd136d1464cf9ef3cc28f0bab68134aa741c1d6 | |
parent | fix issue #55: inform user whan return code is not null (diff) | |
download | lumina-3f8520006b5634cdea0c29953839bff3d7169fd9.tar.gz lumina-3f8520006b5634cdea0c29953839bff3d7169fd9.tar.bz2 lumina-3f8520006b5634cdea0c29953839bff3d7169fd9.zip |
simple editor for .desktop files
-rw-r--r-- | desktop-editor/desktop-app.template | 11 | ||||
-rw-r--r-- | desktop-editor/desktop-editor.pro | 32 | ||||
-rw-r--r-- | desktop-editor/desktop-link.template | 7 | ||||
-rw-r--r-- | desktop-editor/dialog.cpp | 199 | ||||
-rw-r--r-- | desktop-editor/dialog.h | 46 | ||||
-rw-r--r-- | desktop-editor/dialog.ui | 226 | ||||
-rw-r--r-- | desktop-editor/main.cpp | 19 |
7 files changed, 540 insertions, 0 deletions
diff --git a/desktop-editor/desktop-app.template b/desktop-editor/desktop-app.template new file mode 100644 index 00000000..8519d3a1 --- /dev/null +++ b/desktop-editor/desktop-app.template @@ -0,0 +1,11 @@ +[Desktop Entry] +Version=1.0 +Encoding=UTF-8 +Name=Name +Exec=Command +Icon=system-help.png +StartupNotify=false +Terminal=false +Type=Application +Categories=Application +Comment=Comment diff --git a/desktop-editor/desktop-editor.pro b/desktop-editor/desktop-editor.pro new file mode 100644 index 00000000..dc3eadf2 --- /dev/null +++ b/desktop-editor/desktop-editor.pro @@ -0,0 +1,32 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-02-24T18:52:15 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = desktop-editor +TEMPLATE = app + +isEmpty(PREFIX) { + PREFIX = /usr/local +} + +isEmpty(LIBPREFIX) { + LIBPREFIX = $$PREFIX/lib +} + + +SOURCES += main.cpp\ + dialog.cpp + +HEADERS += dialog.h + +FORMS += dialog.ui + +INCLUDEPATH += $$PREFIX/include + +LIBS += -L$$LIBPREFIX -lLuminaUtils diff --git a/desktop-editor/desktop-link.template b/desktop-editor/desktop-link.template new file mode 100644 index 00000000..4a0b7830 --- /dev/null +++ b/desktop-editor/desktop-link.template @@ -0,0 +1,7 @@ +[Desktop Entry] +Version=1.0 +Encoding=UTF-8 +Name=Name +Icon=system-help.png +Type=Link +Comment=Comment diff --git a/desktop-editor/dialog.cpp b/desktop-editor/dialog.cpp new file mode 100644 index 00000000..4b4d066a --- /dev/null +++ b/desktop-editor/dialog.cpp @@ -0,0 +1,199 @@ +#include "dialog.h" +#include "ui_dialog.h" +#include <QFileDialog> +#include <QRegExp> +#include <QTemporaryFile> +#include <QMessageBox> +#include "LuminaUtils.h" + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + ui->setupUi(this); + desktopType="application"; //default value +} + + +Dialog::~Dialog() +{ + delete ui; +} + +//Inform the user that required input parameters are missing +void Dialog::MissingInputs() +{ + qDebug() << "We cannot continue without a desktop file !!!"; + QMessageBox::critical(this, tr("Error"), tr("The application requires inputs: <-application>|<-link> desktopfile") ); + exit(1); +} + + +//Initialise the layout of the screen. +void Dialog::Initialise(QString param) +{ + //"application" is the default layout. + + //in case of "link", several objects are no required + if (param.startsWith("-link")) { + ui->cbRunInTerminal->setVisible(false); + ui->cbStartupNotification->setVisible(false); + ui->lCommand->setVisible(false); + ui->pbCommand->setVisible(false); + ui->label_3->setVisible(false); + ui->label_6->setVisible(false); + ui->label_4->setText("URL"); //we use the WorkingDir boxes for URL + desktopType="link"; + } +} + +//load the desktop file or the required template +void Dialog::LoadDesktopFile(QString input) +{ + //if we have "-" as 1st char, it means that this is not a desktop file, but a parameter + desktopFileName = input; + if (input.startsWith("-")) { + QMessageBox::critical(this,tr("Error"),tr("The filename cannot start with a \"-\".")); + exit(1); + } + + //if proposed file does not exist, than we will create one based on the templates + //TODO: have a config directory to store templates + if (!QFile::exists(input)) { + if (desktopType=="link") { QFile::copy("./desktop-link.template", desktopFileName);} + else { QFile::copy("./desktop-app.template", desktopFileName);} + } + + //use the standard LXDG object and load the desktop file + bool ok = false; + DF = LXDG::loadDesktopFile(desktopFileName, ok); + if( ok ) { + ui->lName->setText(DF.name); + ui->lComment->setText(DF.comment); + ui->lCommand->setText(DF.exec); + //in case of "link" desktop, we populate the correct content in lWorkingDir + if (desktopType=="link") { + ui->lWorkingDir->setText(DF.url); + } else { + ui->lWorkingDir->setText(DF.path); + } + if (DF.startupNotify) ui->cbStartupNotification->setChecked(true); else ui->cbStartupNotification->setChecked(false); + if (DF.useTerminal) ui->cbRunInTerminal->setChecked(true); else ui->cbRunInTerminal->setChecked(false); + iconFileName=""; + ui->pbIcon->setIcon(QPixmap(DF.icon)); + } else { + QMessageBox::critical(this, tr("Error"), tr("Problem to read the desktop file called:") + desktopFileName ); + exit(1); + } +} + + +void Dialog::on_pbCommand_clicked() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open command"), "~", tr("All Files (*)")); + ui->lCommand->setText(fileName); +} + + +void Dialog::on_pbWorkingDir_clicked() +{ + QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly; + QString directory = QFileDialog::getExistingDirectory(this, + tr("Working Directory"), + ui->lWorkingDir->text(), + options); + ui->lWorkingDir->setText(directory); +} + +//this function is just like a regexp. +//we just change the required lines and we don't touch to the rest of the file and copy it back. +void textReplace(QString &origin, QString from, QString to, QString topic) +{ + if (!from.isEmpty()) { + //TODO RegExp with ^ to detect begining of the line is not working + origin.replace(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive),"\n" + topic + "=" + to + "\n"); + } else { + //TODO: check if last char in \n. If not add it + origin.append(topic + "=" + to + "\n"); + } +} + +//we save the changes to the destination file +void Dialog::on_pbApply_clicked() +{ + + QByteArray fileData; + QFile file(desktopFileName); + if (file.open(QFile::ReadWrite)) { + QString from,to; + fileData = file.readAll(); + QString text(fileData); + + QString desktopTypeVal="Application"; + if (DF.type == XDGDesktop::APP) { desktopTypeVal="Application"; } + else if (DF.type == XDGDesktop::LINK) { desktopTypeVal="Link"; } + else if (DF.type == XDGDesktop::DIR) { desktopTypeVal="Dir"; } + textReplace(text, desktopTypeVal, desktopType, "Type"); + + if (ui->lName->isModified()) { textReplace(text, DF.name, ui->lName->text(), "Name");} + if (ui->lComment->isModified()) { textReplace(text, DF.comment, ui->lComment->text(), "Comment");} + if (ui->lCommand->isModified()) { textReplace(text, DF.exec, ui->lCommand->text(),"Exec");} + if (desktopType=="link") { + //incase of "link" layout WorkingDir is corresponding to the URL + if (ui->lWorkingDir->isModified()) { textReplace(text, DF.url, ui->lWorkingDir->text(),"URL");} + } else { + if (ui->lWorkingDir->isModified()) { textReplace(text, DF.path, ui->lWorkingDir->text(),"Path");} + } + if (ui->cbStartupNotification->isChecked() != DF.startupNotify) { + if (DF.startupNotify) {from="true"; to="false";} else {from="false"; to="true";} + textReplace(text, from, to,"StartupNotify"); + } + if (ui->cbRunInTerminal->isChecked() != DF.useTerminal) { + if (DF.useTerminal) {from="true"; to="false";} else {from="false"; to="true";} + textReplace(text, from, to,"Terminal"); + } + if (!iconFileName.isEmpty()) { + from=DF.icon; + to=iconFileName; + textReplace(text, from, to,"Icon"); + } + + file.seek(0); + file.write(text.toUtf8()); + + file.resize(file.pos());//remove possible trailing lines + + file.close(); + + //hack required to update the icon on the desktop + //maybe the solution would be to have a QFileSystemWatcher (in AppLauncherPlugin.cpp) + //on files instead of ~/Desktop + QTemporaryFile tempFile ; + tempFile.setAutoRemove(false); + tempFile.open(); + tempFile.close(); + qDebug() << "temp file:" << tempFile.fileName(); + + //TODO: capture errors + QString cmd = "mv"; + cmd = cmd + " " + desktopFileName + " " + tempFile.fileName(); + qDebug() << "cmd:" << cmd; + int ret = LUtils::runCmd(cmd); + + cmd = "mv"; + cmd = cmd + " " + tempFile.fileName() + " " + desktopFileName; + qDebug() << "cmd:" << cmd; + ret = LUtils::runCmd(cmd); + } +} + + +void Dialog::on_pbIcon_clicked() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open command"), "~", tr("Image Files (*.png *.jpg *.bmp)")); + qDebug() << "icon:" << fileName; + ui->pbIcon->setIcon(QPixmap(fileName)); + iconFileName=fileName; +} diff --git a/desktop-editor/dialog.h b/desktop-editor/dialog.h new file mode 100644 index 00000000..1a53be1a --- /dev/null +++ b/desktop-editor/dialog.h @@ -0,0 +1,46 @@ +#ifndef DIALOG_H +#define DIALOG_H + +#include <QDialog> +#include <LuminaXDG.h> + +namespace Ui { +class Dialog; +} + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = 0); + + XDGDesktop DF ; + + QString desktopFileName ; + QString iconFileName; + QString desktopType; + + void Initialise(QString); + void MissingInputs(); + void LoadDesktopFile(QString); + + + ~Dialog(); + +private slots: + + void on_pbCommand_clicked(); + + void on_pbWorkingDir_clicked(); + + void on_pbApply_clicked(); + + + void on_pbIcon_clicked(); + +private: + Ui::Dialog *ui; +}; + +#endif // DIALOG_H diff --git a/desktop-editor/dialog.ui b/desktop-editor/dialog.ui new file mode 100644 index 00000000..3620fc00 --- /dev/null +++ b/desktop-editor/dialog.ui @@ -0,0 +1,226 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Dialog</class> + <widget class="QDialog" name="Dialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>420</width> + <height>316</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QGridLayout" name="gridLayout"> + <item row="3" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label_4"> + <property name="text"> + <string>Working dir:</string> + </property> + </widget> + </item> + <item row="3" column="2"> + <widget class="QPushButton" name="pbWorkingDir"> + <property name="enabled"> + <bool>true</bool> + </property> + <property name="text"> + <string/> + </property> + <property name="icon"> + <iconset> + <normalon>../../../../usr/local/share/icons/gnome/32x32/places/folder.png</normalon> + </iconset> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QPushButton" name="pbCommand"> + <property name="text"> + <string/> + </property> + <property name="icon"> + <iconset> + <normalon>../../../../usr/local/share/icons/gnome/32x32/actions/gnome-run.png</normalon> + </iconset> + </property> + </widget> + </item> + <item row="5" column="1"> + <widget class="QCheckBox" name="cbStartupNotification"> + <property name="text"> + <string>Use stratup notification</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QLineEdit" name="lWorkingDir"> + <property name="enabled"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="4" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>Icon:</string> + </property> + </widget> + </item> + <item row="2" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Command:</string> + </property> + </widget> + </item> + <item row="1" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Comment:</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLineEdit" name="lCommand"/> + </item> + <item row="4" column="1" alignment="Qt::AlignLeft"> + <widget class="QPushButton" name="pbIcon"> + <property name="maximumSize"> + <size> + <width>275</width> + <height>16777215</height> + </size> + </property> + <property name="text"> + <string/> + </property> + <property name="icon"> + <iconset> + <normalon>../../../../usr/local/share/icons/gnome/32x32/categories/xfce-graphics.png</normalon> + </iconset> + </property> + <property name="iconSize"> + <size> + <width>64</width> + <height>64</height> + </size> + </property> + </widget> + </item> + <item row="6" column="1"> + <widget class="QCheckBox" name="cbRunInTerminal"> + <property name="text"> + <string>Run in terminal</string> + </property> + </widget> + </item> + <item row="0" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Name:</string> + </property> + </widget> + </item> + <item row="5" column="0" alignment="Qt::AlignRight"> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Options</string> + </property> + </widget> + </item> + <item row="0" column="1" colspan="2"> + <widget class="QLineEdit" name="lName"/> + </item> + <item row="1" column="1" colspan="2"> + <widget class="QLineEdit" name="lComment"/> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="pbCancel"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="pbApply"> + <property name="text"> + <string>Apply</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <layoutdefault spacing="6" margin="11"/> + <tabstops> + <tabstop>lName</tabstop> + <tabstop>lComment</tabstop> + <tabstop>lCommand</tabstop> + <tabstop>lWorkingDir</tabstop> + <tabstop>pbIcon</tabstop> + <tabstop>cbStartupNotification</tabstop> + <tabstop>cbRunInTerminal</tabstop> + <tabstop>pbCancel</tabstop> + <tabstop>pbApply</tabstop> + <tabstop>pbCommand</tabstop> + <tabstop>pbWorkingDir</tabstop> + </tabstops> + <resources/> + <connections> + <connection> + <sender>pbCancel</sender> + <signal>clicked()</signal> + <receiver>Dialog</receiver> + <slot>close()</slot> + <hints> + <hint type="sourcelabel"> + <x>289</x> + <y>286</y> + </hint> + <hint type="destinationlabel"> + <x>274</x> + <y>316</y> + </hint> + </hints> + </connection> + <connection> + <sender>pbApply</sender> + <signal>clicked()</signal> + <receiver>Dialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>375</x> + <y>286</y> + </hint> + <hint type="destinationlabel"> + <x>372</x> + <y>318</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/desktop-editor/main.cpp b/desktop-editor/main.cpp new file mode 100644 index 00000000..58cc3620 --- /dev/null +++ b/desktop-editor/main.cpp @@ -0,0 +1,19 @@ +#include <QApplication> +#include "dialog.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Dialog w; + if (argc==2) { + w.LoadDesktopFile(QString(argv[1]).simplified()); + } else if (argc==3) { + w.Initialise(QString(argv[1]).simplified()); + w.LoadDesktopFile(QString(argv[2]).simplified()); + } else { + w.MissingInputs(); + } + w.show(); + + return a.exec(); +} |