File: desktopentrydialog.cpp

package info (click to toggle)
pcmanfm-qt 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,052 kB
  • sloc: cpp: 10,525; xml: 47; makefile: 10
file content (173 lines) | stat: -rw-r--r-- 7,198 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "desktopentrydialog.h"
#include "ui_desktopentrydialog.h"
#include "xdgdir.h"
#include <QFile>
#include <QDir>
#include <QStandardPaths>
#include <QFileDialog>
#include <QWhatsThis>

namespace PCManFM {

DesktopEntryDialog::DesktopEntryDialog(QWidget* parent, const Fm::FilePath& dirPath):
    QDialog(parent),
    dirPath_{dirPath} {
    ui.setupUi(this);

    if(!dirPath_.isValid() || !dirPath_.isNative()) { // Desktop is the default place
        dirPath_ = Fm::FilePath::fromLocalPath(XdgDir::readDesktopDir().toStdString().c_str());
    }

    connect(ui.typeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DesktopEntryDialog::onChangingType);
    connect(ui.iconButton, &QAbstractButton::clicked, this, &DesktopEntryDialog::onClickingIconButton);
    connect(ui.commandButton, &QAbstractButton::clicked, this, &DesktopEntryDialog::onClickingCommandButton);

    connect(ui.buttonBox, &QDialogButtonBox::helpRequested, this, [] {
        QWhatsThis::enterWhatsThisMode();
    });
    onChangingType(0);
}

DesktopEntryDialog::~DesktopEntryDialog() = default;

void DesktopEntryDialog::onChangingType(int type) {
    if(type == 0) {
        ui.commandLabel->setText(tr("Command:"));
        ui.commandEdit->setWhatsThis(tr("The command to execute."));
    }
    else if(type == 1) {
        ui.commandLabel->setText(tr("URL:"));
        ui.commandEdit->setWhatsThis(tr("The URL to access."));
    }
    ui.catLabel->setVisible(type == 0);
    ui.catEdit->setVisible(type == 0);
}

void DesktopEntryDialog::onClickingIconButton() {
    QString iconDir;
    QString iconThemeName = QIcon::themeName();
    QStringList icons = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
                                                  QStringLiteral("icons"),
                                                  QStandardPaths::LocateDirectory);
    for(QStringList::ConstIterator it = icons.constBegin(); it != icons.constEnd(); ++it) {
        QString iconThemeFolder = *it + QLatin1String("/") + iconThemeName;
        if (QDir(iconThemeFolder).exists() && QFileInfo(iconThemeFolder).permission(QFileDevice::ReadUser)) {
            iconDir = iconThemeFolder;
            break;
        }
    }
    if(iconDir.isEmpty()) {
        iconDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
                                         QStringLiteral("icons"),
                                         QStandardPaths::LocateDirectory);
        if(iconDir.isEmpty()) {
            return;
        }
    }
    const QString iconPath = QFileDialog::getOpenFileName(this, tr("Select an icon"),
                                                          iconDir,
                                                          tr("Images (*.png *.xpm *.svg *.svgz )"));
    if(!iconPath.isEmpty()) {
        if(iconPath.startsWith(iconDir)) { // a theme icon
            QStringList parts = iconPath.split(QStringLiteral("/"), Qt::SkipEmptyParts);
            if(!parts.isEmpty()) {
                QString iconName = parts.at(parts.count() - 1);
                int ln = iconName.lastIndexOf(QLatin1String("."));
                if(ln > -1) {
                    iconName.remove(ln, iconName.length() - ln);
                    ui.iconEdit->setText(iconName);
                }
            }
        }
        else { // an image file
            ui.iconEdit->setText(iconPath);
        }
    }
}

void DesktopEntryDialog::onClickingCommandButton() {
    if(ui.typeCombo->currentIndex() == 0) {
        const QString path = QFileDialog::getOpenFileName(this,
                                                          tr("Select an executable file"),
                                                          QString::fromUtf8(Fm::FilePath::homeDir().toString().get()));
        if(!path.isEmpty()) {
            ui.commandEdit->setText(path);
        }
    }
    else {
        const QUrl url = QFileDialog::getOpenFileUrl(this,
                                                     tr("Select a file"),
                                                     QUrl(QString::fromUtf8(Fm::FilePath::homeDir().toString().get())));
        if(!url.isEmpty()) {
            ui.commandEdit->setText(url.toString());
        }
    }
}

void DesktopEntryDialog::accept() {
    QString name = ui.nameEdit->text();
    if(name.isEmpty()) {
        name = QLatin1String("launcher");
    }
    GKeyFile* kf = g_key_file_new();
    g_key_file_set_string(kf, "Desktop Entry", "Name", name.toStdString().c_str());
    g_key_file_set_string(kf, "Desktop Entry", "GenericName", ui.descriptionEdit->text().toStdString().c_str());
    g_key_file_set_string(kf, "Desktop Entry", "Comment", ui.commentEdit->text().toStdString().c_str());
    if(ui.typeCombo->currentIndex() == 0) {
        g_key_file_set_string(kf, "Desktop Entry", "Exec", ui.commandEdit->text().toStdString().c_str());
        g_key_file_set_string(kf, "Desktop Entry", "Type", "Application");

        // categories
        auto categories = ui.catEdit->text();
        if(!categories.isEmpty()) {
            if(!categories.endsWith(QLatin1Char(';'))) {
                categories.append(QLatin1Char(';'));
            }
            g_key_file_set_string(kf, "Desktop Entry", "Categories", categories.toStdString().c_str());
        }
    }
    else {
        QString cmd = ui.commandEdit->text();
        // correct the type if this is a special place
        if(cmd.startsWith(QLatin1String("computer:///"))
           || cmd.startsWith(QLatin1String("network:///"))
           || cmd.startsWith(QLatin1String("trash:///"))
           || cmd.startsWith(QLatin1String("menu://applications/"))) {
            cmd = QLatin1String("pcmanfm-qt ") + cmd;
            g_key_file_set_string(kf, "Desktop Entry", "Exec", cmd.toStdString().c_str());
            g_key_file_set_string(kf, "Desktop Entry", "Type", "Application");
        }
        else {
            g_key_file_set_string(kf, "Desktop Entry", "URL", cmd.toStdString().c_str());
            g_key_file_set_string(kf, "Desktop Entry", "Type", "Link");
        }
    }
    g_key_file_set_string(kf, "Desktop Entry", "Icon", ui.iconEdit->text().toStdString().c_str());
    g_key_file_set_string(kf, "Desktop Entry", "Terminal",
                          ui.terminalCombo->currentIndex() == 0 ? "false" : "true");

    auto pathStrPtr = dirPath_.toString();

    // make file name from entry name but so that it doesn't exist on Desktop
    name = name.simplified();
    name.replace(QChar(QChar::Space), QLatin1Char('_'));
    name.replace(QLatin1Char('/'), QLatin1Char('_'));
    QString suffix;
    int i = 0;
    while(QFile::exists(QString::fromUtf8(pathStrPtr.get())
                        + QLatin1String("/") + name + suffix + QLatin1String(".desktop"))) {
        suffix = QString::number(i);
        i++;
    }
    name += suffix + QLatin1String(".desktop");

    auto launcher = Fm::CStrPtr{g_build_filename(pathStrPtr.get(), name.toStdString().c_str(), nullptr)};
    if(g_key_file_save_to_file(kf, launcher.get(), nullptr)) {
        Q_EMIT desktopEntryCreated(dirPath_, name);
    }
    g_key_file_free(kf);

    QDialog::accept();
}

} // namespace Fm