From c168111955fdaac4b546e82138de5e75545cfb07 Mon Sep 17 00:00:00 2001 From: william Date: Thu, 26 Mar 2015 21:54:09 +0100 Subject: put the name lumina-fileinfo --- lumina-fileinfo/dialog.cpp | 239 ++++++++++++++++++++++++++++++++ lumina-fileinfo/dialog.h | 46 ++++++ lumina-fileinfo/dialog.ui | 226 ++++++++++++++++++++++++++++++ lumina-fileinfo/fileinfo-app.template | 11 ++ lumina-fileinfo/fileinfo-link.template | 7 + lumina-fileinfo/lumina-fileinfo.desktop | 10 ++ lumina-fileinfo/lumina-fileinfo.pro | 40 ++++++ lumina-fileinfo/main.cpp | 20 +++ 8 files changed, 599 insertions(+) create mode 100644 lumina-fileinfo/dialog.cpp create mode 100644 lumina-fileinfo/dialog.h create mode 100644 lumina-fileinfo/dialog.ui create mode 100644 lumina-fileinfo/fileinfo-app.template create mode 100644 lumina-fileinfo/fileinfo-link.template create mode 100644 lumina-fileinfo/lumina-fileinfo.desktop create mode 100644 lumina-fileinfo/lumina-fileinfo.pro create mode 100644 lumina-fileinfo/main.cpp (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp new file mode 100644 index 00000000..bcb3a14d --- /dev/null +++ b/lumina-fileinfo/dialog.cpp @@ -0,0 +1,239 @@ +#include "dialog.h" +#include "ui_dialog.h" +#include +#include +#include +#include +#include "LuminaUtils.h" +#include + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + ui->setupUi(this); + desktopType="Application"; //default value + + //Setup all the icons using libLumina + this->setWindowIcon( LXDG::findIcon("preferences-desktop-default-applications","") ); + ui->pbWorkingDir->setIcon( LXDG::findIcon("folder","") ); + ui->pbCommand->setIcon( LXDG::findIcon("system-search","") ); +} + + +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) +{ + + //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->lblCommand->setVisible(false); + ui->lblOptions->setVisible(false); + ui->lblWorkingDir->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("./fileinfo-link.template", desktopFileName);} + else { QFile::copy("./fileinfo-app.template", desktopFileName);} + } + + //use the standard LXDG object and load the desktop file + bool ok = false; + DF = LXDG::loadDesktopFile(desktopFileName, ok); + if( ok ) { + if ((DF.type == XDGDesktop::LINK) && (desktopType!="link" )) { + //we open a desktop type "link" but it was not mentionned by parameters + Dialog::Initialise("-link"); + } + 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() +{ + //the default directory is the user's home directory + QString commandFolder = "~"; + if (!ui->lCommand->text().isEmpty()) commandFolder = ui->lCommand->text().section('/', 0, -2); + if (commandFolder.isEmpty()) commandFolder = "~"; + + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open command"), commandFolder, tr("All Files (*)")); + if (!fileName.isEmpty()) { + ui->lCommand->setText(fileName); + ui->lCommand->setModified(true); + } +} + + +void Dialog::on_pbWorkingDir_clicked() +{ + //the default directory is / + QString workingDir = "/"; + if (ui->lWorkingDir->text().isEmpty()) workingDir = "/"; + else workingDir = ui->lWorkingDir->text(); + QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly; + QString directory = QFileDialog::getExistingDirectory(this, + tr("Working Directory"), + workingDir, + options); + if (!directory.isEmpty()) { + ui->lWorkingDir->setText(directory); + ui->lWorkingDir->setModified(true); + } +} + +//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 Dialog::textReplace(QString &origin, QString from, QString to, QString topic) +{ + if (origin.contains(QRegExp("\n" + topic + "\\[\\S+\\]\\s*=",Qt::CaseInsensitive))) { + QMessageBox msgBox; + msgBox.setText(tr("By modifying this value, you will loose all translated versions")); + msgBox.setInformativeText(tr("The field:") + topic + tr( "is translated in several other languages. If you want to continue, you will loose all translated versions")); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + int answer = msgBox.exec(); + if (answer==QMessageBox::Ok) { + //remove all translated versions. The lang cannot be null, but the value can be. + origin.replace(QRegExp("\n" + topic + "\\[\\S+\\]\\s*=[^\n]*",Qt::CaseInsensitive), ""); + } + else return; + } + if (!from.isEmpty()) { + 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 + QTemporaryFile tempFile ; + tempFile.setAutoRemove(false); + tempFile.open(); + tempFile.close(); + + //TODO: capture errors + QString cmd = "mv"; + cmd = cmd + " " + desktopFileName + " " + tempFile.fileName(); + int ret = LUtils::runCmd(cmd); + + cmd = "mv"; + cmd = cmd + " " + tempFile.fileName() + " " + desktopFileName; + ret = LUtils::runCmd(cmd); + } +} + + +void Dialog::on_pbIcon_clicked() +{ + //the default directory is local/share/icons + QString iconFolder = LOS::AppPrefix()+"/share/icons"; + if (!iconFileName.isEmpty()) iconFolder = iconFileName.section('/', 0, -2); + else if (!DF.icon.isEmpty()) iconFolder = DF.icon.section('/', 0, -2); + if (iconFolder.isEmpty()) iconFolder = LOS::AppPrefix()+"/share/icons"; + + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open command"), iconFolder, tr("Image Files (*.png *.jpg *.bmp)")); + if (!fileName.isEmpty()) { + ui->pbIcon->setIcon(QPixmap(fileName)); + iconFileName=fileName; + } +} diff --git a/lumina-fileinfo/dialog.h b/lumina-fileinfo/dialog.h new file mode 100644 index 00000000..e58f0938 --- /dev/null +++ b/lumina-fileinfo/dialog.h @@ -0,0 +1,46 @@ +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include + +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(); + + void textReplace(QString &origin, QString from, QString to, QString topic); + +private: + Ui::Dialog *ui; +}; + +#endif // DIALOG_H diff --git a/lumina-fileinfo/dialog.ui b/lumina-fileinfo/dialog.ui new file mode 100644 index 00000000..8cca25d4 --- /dev/null +++ b/lumina-fileinfo/dialog.ui @@ -0,0 +1,226 @@ + + + Dialog + + + + 0 + 0 + 383 + 288 + + + + Desktop Editor + + + + + + + + Working dir: + + + + + + + true + + + + + + + ../../../../usr/local/share/icons/gnome/32x32/places/folder.png + + + + + + + + + + + + ../../../../usr/local/share/icons/gnome/32x32/actions/gnome-run.png + + + + + + + + Use startup notification + + + + + + + true + + + + + + + Icon: + + + + + + + Command: + + + + + + + Comment: + + + + + + + + + + + 275 + 16777215 + + + + + + + + ../../../../usr/local/share/icons/gnome/32x32/categories/xfce-graphics.png + + + + + 64 + 64 + + + + + + + + Run in terminal + + + + + + + Name: + + + + + + + Options + + + + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Cancel + + + + + + + Apply + + + + + + + + + + lName + lComment + lCommand + lWorkingDir + pbIcon + cbStartupNotification + cbRunInTerminal + pbCancel + pbApply + pbCommand + pbWorkingDir + + + + + pbCancel + clicked() + Dialog + close() + + + 289 + 286 + + + 274 + 316 + + + + + pbApply + clicked() + Dialog + accept() + + + 375 + 286 + + + 372 + 318 + + + + + diff --git a/lumina-fileinfo/fileinfo-app.template b/lumina-fileinfo/fileinfo-app.template new file mode 100644 index 00000000..8519d3a1 --- /dev/null +++ b/lumina-fileinfo/fileinfo-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/lumina-fileinfo/fileinfo-link.template b/lumina-fileinfo/fileinfo-link.template new file mode 100644 index 00000000..4a0b7830 --- /dev/null +++ b/lumina-fileinfo/fileinfo-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/lumina-fileinfo/lumina-fileinfo.desktop b/lumina-fileinfo/lumina-fileinfo.desktop new file mode 100644 index 00000000..d5b5ddf9 --- /dev/null +++ b/lumina-fileinfo/lumina-fileinfo.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Exec=/home/wi/nas/github/lumina/desktop-editor/desktop-editor +Icon=/usr/local/share/pixmaps/Lumina-DE.png +Terminal=false +Type=Application +StartupNotify=true +Categories=System; +Name=Lumina Desktop Information +Comment=View information about the Lumina Desktop Environment + diff --git a/lumina-fileinfo/lumina-fileinfo.pro b/lumina-fileinfo/lumina-fileinfo.pro new file mode 100644 index 00000000..9524f9ef --- /dev/null +++ b/lumina-fileinfo/lumina-fileinfo.pro @@ -0,0 +1,40 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-02-24T18:52:15 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = lumina-fileinfo +TEMPLATE = app + +isEmpty(PREFIX) { + PREFIX = /usr/local +} +target.path = $$PREFIX/bin + +isEmpty(LIBPREFIX) { + LIBPREFIX = $$PREFIX/lib +} + + +SOURCES += main.cpp\ + dialog.cpp + +HEADERS += dialog.h + +FORMS += dialog.ui + +RESOURCES+= lumina-fileinfo.qrc + +INCLUDEPATH += $$PREFIX/include + +LIBS += -L$$LIBPREFIX -lLuminaUtils + +desktop.files=lumina-search.desktop +desktop.path=$$PREFIX/share/applications/ + +INSTALLS += target desktop diff --git a/lumina-fileinfo/main.cpp b/lumina-fileinfo/main.cpp new file mode 100644 index 00000000..deefd46f --- /dev/null +++ b/lumina-fileinfo/main.cpp @@ -0,0 +1,20 @@ +#include +#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(); + } + a.setApplicationName("Lumina File Info"); + w.show(); + + return a.exec(); +} -- cgit From 029629ee702f9eb2babc21cfcc3322cfc734f4ce Mon Sep 17 00:00:00 2001 From: wi Date: Sun, 29 Mar 2015 22:14:14 +0200 Subject: have a config directory to store templates --- lumina-fileinfo/defaults/fileinfo-app.template | 11 ++++++++ lumina-fileinfo/defaults/fileinfo-link.template | 7 +++++ lumina-fileinfo/dialog.cpp | 35 ++++++++++++++++++++++--- lumina-fileinfo/fileinfo-app.template | 11 -------- lumina-fileinfo/fileinfo-link.template | 7 ----- lumina-fileinfo/lumina-fileinfo.qrc | 6 +++++ 6 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 lumina-fileinfo/defaults/fileinfo-app.template create mode 100644 lumina-fileinfo/defaults/fileinfo-link.template delete mode 100644 lumina-fileinfo/fileinfo-app.template delete mode 100644 lumina-fileinfo/fileinfo-link.template create mode 100644 lumina-fileinfo/lumina-fileinfo.qrc (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/defaults/fileinfo-app.template b/lumina-fileinfo/defaults/fileinfo-app.template new file mode 100644 index 00000000..8519d3a1 --- /dev/null +++ b/lumina-fileinfo/defaults/fileinfo-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/lumina-fileinfo/defaults/fileinfo-link.template b/lumina-fileinfo/defaults/fileinfo-link.template new file mode 100644 index 00000000..4a0b7830 --- /dev/null +++ b/lumina-fileinfo/defaults/fileinfo-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/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp index bcb3a14d..30d80064 100644 --- a/lumina-fileinfo/dialog.cpp +++ b/lumina-fileinfo/dialog.cpp @@ -18,6 +18,20 @@ Dialog::Dialog(QWidget *parent) : this->setWindowIcon( LXDG::findIcon("preferences-desktop-default-applications","") ); ui->pbWorkingDir->setIcon( LXDG::findIcon("folder","") ); ui->pbCommand->setIcon( LXDG::findIcon("system-search","") ); + + //we copy qrc templates in the home dir of the user. + //this allow the user to adapt those template to is own whishes + QString templateFile = QDir::homePath() + "/.lumina/LuminaDE/fileinfo-link.template"; + if (!QFile::exists(templateFile)) { + QFile::copy(":defaults/fileinfo-link.template", templateFile); + QFile(templateFile).setPermissions(QFileDevice::ReadUser|QFileDevice::WriteUser); + } + templateFile = QDir::homePath() + "/.lumina/LuminaDE/fileinfo-app.template"; + if (!QFile::exists(templateFile)) { + QFile::copy(":defaults/fileinfo-app.template", templateFile); + QFile(templateFile).setPermissions(QFileDevice::ReadUser|QFileDevice::WriteUser); + } + } @@ -49,7 +63,7 @@ void Dialog::Initialise(QString param) ui->lblOptions->setVisible(false); ui->lblWorkingDir->setText("URL"); //we use the WorkingDir boxes for URL desktopType="link"; - } + } } @@ -66,10 +80,23 @@ void Dialog::LoadDesktopFile(QString input) } //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("./fileinfo-link.template", desktopFileName);} - else { QFile::copy("./fileinfo-app.template", desktopFileName);} + if (desktopType=="link") { + if (QFile::exists(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-link.template")) { + //We take the template from homedir + QFile::copy(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-link.template", desktopFileName); + } else { + //last possibility os to use the qrc template. + //But based on the initialisation, this should never occurs + QFile::copy(":defaults/fileinfo-link.template", desktopFileName); + } + } else { + if (QFile::exists(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-app.template")) { + QFile::copy(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-app.template", desktopFileName); + } else { + QFile::copy(":defaults/fileinfo-app.template", desktopFileName); + } + } } //use the standard LXDG object and load the desktop file diff --git a/lumina-fileinfo/fileinfo-app.template b/lumina-fileinfo/fileinfo-app.template deleted file mode 100644 index 8519d3a1..00000000 --- a/lumina-fileinfo/fileinfo-app.template +++ /dev/null @@ -1,11 +0,0 @@ -[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/lumina-fileinfo/fileinfo-link.template b/lumina-fileinfo/fileinfo-link.template deleted file mode 100644 index 4a0b7830..00000000 --- a/lumina-fileinfo/fileinfo-link.template +++ /dev/null @@ -1,7 +0,0 @@ -[Desktop Entry] -Version=1.0 -Encoding=UTF-8 -Name=Name -Icon=system-help.png -Type=Link -Comment=Comment diff --git a/lumina-fileinfo/lumina-fileinfo.qrc b/lumina-fileinfo/lumina-fileinfo.qrc new file mode 100644 index 00000000..4667589a --- /dev/null +++ b/lumina-fileinfo/lumina-fileinfo.qrc @@ -0,0 +1,6 @@ + + + defaults/fileinfo-app.template + defaults/fileinfo-link.template + + -- cgit From 64f8f190c27f5880982353a7dac57b92793090b1 Mon Sep 17 00:00:00 2001 From: wi Date: Sun, 12 Apr 2015 21:59:39 +0200 Subject: refactring of lumina-fileinfo: - detect if a field exist in different language and inform the user - add the inMemoryFile which contains the whole lines of the config. every modifications are stored in there and once the user click "apply" the whole content goes to disk - adapt the changes requested by the user in the inMemoryFile (no more when the user click on "Apply"). --- lumina-fileinfo/dialog.cpp | 159 ++++++++++++++++++++++++++------------------- lumina-fileinfo/dialog.h | 4 ++ 2 files changed, 96 insertions(+), 67 deletions(-) (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp index 30d80064..4b21e9cd 100644 --- a/lumina-fileinfo/dialog.cpp +++ b/lumina-fileinfo/dialog.cpp @@ -31,7 +31,6 @@ Dialog::Dialog(QWidget *parent) : QFile::copy(":defaults/fileinfo-app.template", templateFile); QFile(templateFile).setPermissions(QFileDevice::ReadUser|QFileDevice::WriteUser); } - } @@ -124,6 +123,14 @@ void Dialog::LoadDesktopFile(QString input) QMessageBox::critical(this, tr("Error"), tr("Problem to read the desktop file called:") + desktopFileName ); exit(1); } + + //we load the file in memory and will adapt it before saving it to disk + QFile file(desktopFileName); + inMemoryFile=""; + if (file.open(QFile::ReadWrite)) { + QTextStream fileData(&file); + inMemoryFile = fileData.readAll(); + } } @@ -164,22 +171,9 @@ void Dialog::on_pbWorkingDir_clicked() //we just change the required lines and we don't touch to the rest of the file and copy it back. void Dialog::textReplace(QString &origin, QString from, QString to, QString topic) { - if (origin.contains(QRegExp("\n" + topic + "\\[\\S+\\]\\s*=",Qt::CaseInsensitive))) { - QMessageBox msgBox; - msgBox.setText(tr("By modifying this value, you will loose all translated versions")); - msgBox.setInformativeText(tr("The field:") + topic + tr( "is translated in several other languages. If you want to continue, you will loose all translated versions")); - msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); - int answer = msgBox.exec(); - if (answer==QMessageBox::Ok) { - //remove all translated versions. The lang cannot be null, but the value can be. - origin.replace(QRegExp("\n" + topic + "\\[\\S+\\]\\s*=[^\n]*",Qt::CaseInsensitive), ""); - } - else return; - } - if (!from.isEmpty()) { + if (origin.contains(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive))) { 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"); } } @@ -187,65 +181,62 @@ void Dialog::textReplace(QString &origin, QString from, QString to, QString topi //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"); + QString from,to; + 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(inMemoryFile, 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"); - } + if (ui->lName->isModified()) { textReplace(inMemoryFile, DF.name, ui->lName->text(), "Name");} + if (ui->lComment->isModified()) { textReplace(inMemoryFile, DF.comment, ui->lComment->text(), "Comment");} + if (ui->lCommand->isModified()) { textReplace(inMemoryFile, DF.exec, ui->lCommand->text(),"Exec");} + if (desktopType=="link") { + //incase of "link" layout WorkingDir is corresponding to the URL + if (ui->lWorkingDir->isModified()) { textReplace(inMemoryFile, DF.url, ui->lWorkingDir->text(),"URL");} + } else { + if (ui->lWorkingDir->isModified()) { textReplace(inMemoryFile, 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(inMemoryFile, from, to,"StartupNotify"); + } + if (ui->cbRunInTerminal->isChecked() != DF.useTerminal) { + if (DF.useTerminal) {from="true"; to="false";} else {from="false"; to="true";} + textReplace(inMemoryFile, from, to,"Terminal"); + } + if (!iconFileName.isEmpty()) { + from=DF.icon; + to=iconFileName; + textReplace(inMemoryFile, from, to,"Icon"); + } - file.seek(0); - file.write(text.toUtf8()); + QFile file(desktopFileName); + if (file.open(QFile::ReadWrite)) { + file.seek(0); + file.write(inMemoryFile.toUtf8()); - file.resize(file.pos());//remove possible trailing lines + file.resize(file.pos());//remove possible trailing lines - file.close(); + file.close(); + } else { + //problem to write to the disk + } - //hack required to update the icon on the desktop - QTemporaryFile tempFile ; - tempFile.setAutoRemove(false); - tempFile.open(); - tempFile.close(); + //hack required to update the icon on the desktop + QTemporaryFile tempFile ; + tempFile.setAutoRemove(false); + tempFile.open(); + tempFile.close(); - //TODO: capture errors - QString cmd = "mv"; - cmd = cmd + " " + desktopFileName + " " + tempFile.fileName(); - int ret = LUtils::runCmd(cmd); + //TODO: capture errors + QString cmd = "mv"; + cmd = cmd + " " + desktopFileName + " " + tempFile.fileName(); + int ret = LUtils::runCmd(cmd); - cmd = "mv"; - cmd = cmd + " " + tempFile.fileName() + " " + desktopFileName; - ret = LUtils::runCmd(cmd); - } + cmd = "mv"; + cmd = cmd + " " + tempFile.fileName() + " " + desktopFileName; + ret = LUtils::runCmd(cmd); } @@ -264,3 +255,37 @@ void Dialog::on_pbIcon_clicked() iconFileName=fileName; } } + +void Dialog::on_lName_textChanged(QString text) +{ + if (text != DF.name && inMemoryFile.contains(QRegExp("\nName\\[\\S+\\]\\s*=",Qt::CaseInsensitive))) { + QMessageBox msgBox; + msgBox.setText(tr("By modifying this value, you will loose all translated versions")); + msgBox.setInformativeText(tr("The field: Name is translated in several other languages. If you want to continue, you will loose all translated versions")); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + int answer = msgBox.exec(); + if (answer==QMessageBox::Ok) { + //remove all translated versions. The lang cannot be null, but the value can be. + inMemoryFile.replace(QRegExp("\nName\\[\\S+\\]\\s*=[^\n]*",Qt::CaseInsensitive), ""); + } else { + ui->lName->setText(DF.name); + } + } +} + +void Dialog::on_lComment_textChanged(QString text) +{ + if (text != DF.name && inMemoryFile.contains(QRegExp("\nComment\\[\\S+\\]\\s*=",Qt::CaseInsensitive))) { + QMessageBox msgBox; + msgBox.setText(tr("By modifying this value, you will loose all translated versions")); + msgBox.setInformativeText(tr("The field: Comment is translated in several other languages. If you want to continue, you will loose all translated versions")); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + int answer = msgBox.exec(); + if (answer==QMessageBox::Ok) { + //remove all translated versions. The lang cannot be null, but the value can be. + inMemoryFile.replace(QRegExp("\nComment\\[\\S+\\]\\s*=[^\n]*",Qt::CaseInsensitive), ""); + } else { + ui->lName->setText(DF.comment); + } + } +} diff --git a/lumina-fileinfo/dialog.h b/lumina-fileinfo/dialog.h index e58f0938..1dfbd64c 100644 --- a/lumina-fileinfo/dialog.h +++ b/lumina-fileinfo/dialog.h @@ -16,6 +16,7 @@ public: explicit Dialog(QWidget *parent = 0); XDGDesktop DF ; + QString inMemoryFile; QString desktopFileName ; QString iconFileName; @@ -38,6 +39,9 @@ private slots: void on_pbIcon_clicked(); void textReplace(QString &origin, QString from, QString to, QString topic); + + void on_lName_textChanged(QString text); + void on_lComment_textChanged(QString text); private: Ui::Dialog *ui; -- cgit From a03d6e3672cd4282e11afec117f5d0407c8a3ac9 Mon Sep 17 00:00:00 2001 From: wi Date: Tue, 14 Apr 2015 22:07:49 +0200 Subject: Corrections proposed by Ken --- lumina-fileinfo/dialog.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp index 4b21e9cd..14113299 100644 --- a/lumina-fileinfo/dialog.cpp +++ b/lumina-fileinfo/dialog.cpp @@ -118,7 +118,7 @@ void Dialog::LoadDesktopFile(QString input) 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)); + ui->pbIcon->setIcon(LXDG::findIcon(DF.icon,"")); } else { QMessageBox::critical(this, tr("Error"), tr("Problem to read the desktop file called:") + desktopFileName ); exit(1); @@ -137,9 +137,9 @@ void Dialog::LoadDesktopFile(QString input) void Dialog::on_pbCommand_clicked() { //the default directory is the user's home directory - QString commandFolder = "~"; + QString commandFolder = QDir::homePath(); if (!ui->lCommand->text().isEmpty()) commandFolder = ui->lCommand->text().section('/', 0, -2); - if (commandFolder.isEmpty()) commandFolder = "~"; + if (commandFolder.isEmpty()) commandFolder = QDir::homePath(); QString fileName = QFileDialog::getOpenFileName(this, tr("Open command"), commandFolder, tr("All Files (*)")); -- cgit From 23629784d15d669aea5bf71abfaf775931e2324f Mon Sep 17 00:00:00 2001 From: wi Date: Fri, 17 Apr 2015 15:04:05 +0200 Subject: This is a complete adaptation of lumina-fileinfo. The most important element is that during the loadDEsktopFile we check the content of the file. In case the file does not contains the mandatory elements (cfr http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html) we reject it and ask the user to manually (via an anoter tool) correct it, or to restart from scratch. --- lumina-fileinfo/dialog.cpp | 113 ++++++++++++++++++++++++++++++++++----------- lumina-fileinfo/dialog.h | 4 +- 2 files changed, 87 insertions(+), 30 deletions(-) (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp index 14113299..b0217db8 100644 --- a/lumina-fileinfo/dialog.cpp +++ b/lumina-fileinfo/dialog.cpp @@ -7,6 +7,40 @@ #include "LuminaUtils.h" #include + + +//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 Dialog::textReplace(QString &origin, QString from, QString to, QString topic) +{ + if (origin.contains(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive))) { + origin.replace(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive),"\n" + topic + "=" + to + "\n"); + } else { + origin.append(topic + "=" + to + "\n"); + } +} + +//get the template from the user home directory or from the qrc files +void Dialog::copyTemplate(QString templateType) +{ + if ((templateType == "-link") or (templateType == "-app")) { + if (QFile::exists(QDir::homePath() + "/.lumina/LuminaDE/fileinfo" + templateType + ".template")) { + //We take the template from homedir + QFile::copy(QDir::homePath() + "/.lumina/LuminaDE/fileinfo" + templateType + ".template", desktopFileName); + } else { + //last possibility is to use the qrc template. + //But based on the initialisation, this should never occurs + QFile::copy(":defaults/fileinfo" + templateType + ".template", desktopFileName); + } + } else { + //error message for developpers + qDebug() << "copyTemplate only accept '-link' or '-app' as parameter"; + } +} + + + + Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) @@ -60,9 +94,19 @@ void Dialog::Initialise(QString param) ui->pbCommand->setVisible(false); ui->lblCommand->setVisible(false); ui->lblOptions->setVisible(false); - ui->lblWorkingDir->setText("URL"); //we use the WorkingDir boxes for URL + ui->lblWorkingDir->setText(tr("URL")); //we use the WorkingDir boxes for URL desktopType="link"; } + if (param.startsWith("-app")) { + ui->cbRunInTerminal->setVisible(true); + ui->cbStartupNotification->setVisible(true); + ui->lCommand->setVisible(true); + ui->pbCommand->setVisible(true); + ui->lblCommand->setVisible(true); + ui->lblOptions->setVisible(true); + ui->lblWorkingDir->setText(tr("Working dir")); + desktopType="app"; + } } @@ -81,20 +125,9 @@ void Dialog::LoadDesktopFile(QString input) //if proposed file does not exist, than we will create one based on the templates if (!QFile::exists(input)) { if (desktopType=="link") { - if (QFile::exists(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-link.template")) { - //We take the template from homedir - QFile::copy(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-link.template", desktopFileName); - } else { - //last possibility os to use the qrc template. - //But based on the initialisation, this should never occurs - QFile::copy(":defaults/fileinfo-link.template", desktopFileName); - } + copyTemplate("-link"); } else { - if (QFile::exists(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-app.template")) { - QFile::copy(QDir::homePath() + "/.lumina/LuminaDE/fileinfo-app.template", desktopFileName); - } else { - QFile::copy(":defaults/fileinfo-app.template", desktopFileName); - } + copyTemplate("-app"); } } @@ -127,9 +160,44 @@ void Dialog::LoadDesktopFile(QString input) //we load the file in memory and will adapt it before saving it to disk QFile file(desktopFileName); inMemoryFile=""; - if (file.open(QFile::ReadWrite)) { + if (file.open(QFile::ReadOnly)) { QTextStream fileData(&file); inMemoryFile = fileData.readAll(); + file.close(); + //perform some validation checks + //TODO: put those Regex in a config file. + //this will allow checks improvements without compilation of the file + if ((inMemoryFile.contains(QRegExp(".*\\[Desktop Entry\\].*\n"))) && + (inMemoryFile.contains(QRegExp("\n\\s*Type\\s*=.*\n"))) && + (inMemoryFile.contains(QRegExp("\n\\s*Name\\s*=.*\n")))) { + //qDebug() << "sounds a good file"; + } else { + //qDebug() << "wrong file!!!!"; + QMessageBox msgBox; + msgBox.setIcon(QMessageBox::Question); + msgBox.setText(tr("There are some issues with this file !!!!")); + msgBox.setInformativeText(tr("Either you correct this file your self with an editor, or you start from scratch using the link or app template.\nPlease note that this process will update the file called:") + desktopFileName); + QPushButton *linkButton = msgBox.addButton("Link",QMessageBox::AcceptRole); + QPushButton *appButton = msgBox.addButton("App",QMessageBox::ResetRole); + QPushButton *cancelButton = msgBox.addButton("Cancel",QMessageBox::NoRole); + msgBox.exec(); + if (msgBox.clickedButton() == linkButton) { + QFile::remove(desktopFileName); + copyTemplate("-link"); + Initialise("-link"); + LoadDesktopFile(desktopFileName); + } + if (msgBox.clickedButton() == appButton) { + QFile::remove(desktopFileName); + copyTemplate("-app"); + Initialise("-app"); + LoadDesktopFile(desktopFileName); + } + if (msgBox.clickedButton() == cancelButton) { + //we stop here + exit(0); + } + } } } @@ -167,17 +235,6 @@ void Dialog::on_pbWorkingDir_clicked() } } -//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 Dialog::textReplace(QString &origin, QString from, QString to, QString topic) -{ - if (origin.contains(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive))) { - origin.replace(QRegExp("\n" + topic + "\\s*=\\s*" + from + "\n",Qt::CaseInsensitive),"\n" + topic + "=" + to + "\n"); - } else { - origin.append(topic + "=" + to + "\n"); - } -} - //we save the changes to the destination file void Dialog::on_pbApply_clicked() { @@ -221,6 +278,7 @@ void Dialog::on_pbApply_clicked() file.close(); } else { //problem to write to the disk + QMessageBox::critical(this, tr("Problem to write to disk"), tr("We have a problem to write the adapted desktop file to the disk. Can you re-try the modification after solving the issue with the disk ?")); } //hack required to update the icon on the desktop @@ -269,7 +327,7 @@ void Dialog::on_lName_textChanged(QString text) inMemoryFile.replace(QRegExp("\nName\\[\\S+\\]\\s*=[^\n]*",Qt::CaseInsensitive), ""); } else { ui->lName->setText(DF.name); - } + } } } @@ -289,3 +347,4 @@ void Dialog::on_lComment_textChanged(QString text) } } } + diff --git a/lumina-fileinfo/dialog.h b/lumina-fileinfo/dialog.h index 1dfbd64c..cfc9fde7 100644 --- a/lumina-fileinfo/dialog.h +++ b/lumina-fileinfo/dialog.h @@ -31,14 +31,12 @@ public: private slots: void on_pbCommand_clicked(); - void on_pbWorkingDir_clicked(); - void on_pbApply_clicked(); - void on_pbIcon_clicked(); void textReplace(QString &origin, QString from, QString to, QString topic); + void copyTemplate(QString templateType); void on_lName_textChanged(QString text); void on_lComment_textChanged(QString text); -- cgit From c262492c368accde32b290fcbc0d03e465aeb9df Mon Sep 17 00:00:00 2001 From: wi Date: Mon, 20 Apr 2015 22:29:30 +0200 Subject: test the return code of the command to mv file to refresh the icon. --- lumina-fileinfo/dialog.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lumina-fileinfo') diff --git a/lumina-fileinfo/dialog.cpp b/lumina-fileinfo/dialog.cpp index b0217db8..0c7c3a6a 100644 --- a/lumina-fileinfo/dialog.cpp +++ b/lumina-fileinfo/dialog.cpp @@ -284,17 +284,26 @@ void Dialog::on_pbApply_clicked() //hack required to update the icon on the desktop QTemporaryFile tempFile ; tempFile.setAutoRemove(false); + tempFile.setFileTemplate("/tmp/lumina-XXXXXX"); tempFile.open(); tempFile.close(); - //TODO: capture errors QString cmd = "mv"; cmd = cmd + " " + desktopFileName + " " + tempFile.fileName(); int ret = LUtils::runCmd(cmd); + if (ret !=0 ) { + qDebug() << "Problem to execute:" << cmd; + QMessageBox::critical(this, tr("Problem to write to disk"), tr("We have a problem to execute the following command:") + cmd); + + } cmd = "mv"; cmd = cmd + " " + tempFile.fileName() + " " + desktopFileName; ret = LUtils::runCmd(cmd); + if (ret !=0 ) { + qDebug() << "Problem to execute:" << cmd; + QMessageBox::critical(this, tr("Problem to write to disk"), tr("We have a problem to execute the following command:") + cmd); + } } -- cgit