]> git.sur5r.net Git - minitube/blob - src/downloadsettings.cpp
Imported Upstream version 1.7
[minitube] / src / downloadsettings.cpp
1 #include "downloadsettings.h"
2 #include "downloadmanager.h"
3
4 DownloadSettings::DownloadSettings(QWidget *parent) : QWidget(parent) {
5
6     QBoxLayout *layout = new QHBoxLayout(this);
7     layout->setMargin(10);
8
9     message = new QLabel(this);
10     message->setOpenExternalLinks(true);
11     layout->addWidget(message);
12
13     changeFolderButton = new QPushButton(this);
14     changeFolderButton->setText(tr("Change location..."));
15     changeFolderButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
16     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
17     layout->addWidget(changeFolderButton);
18
19     updateMessage();
20 }
21
22 void DownloadSettings::paintEvent(QPaintEvent * /*event*/) {
23     QPainter painter(this);
24 #ifdef APP_MAC
25     QBrush brush;
26     if (window()->isActiveWindow()) {
27         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
28     } else {
29         brush = palette().window();
30     }
31     painter.fillRect(0, 0, width(), height(), brush);
32 #endif
33     painter.setPen(palette().color(QPalette::Mid));
34     painter.drawLine(0, 0, width(), 0);
35 }
36
37 void DownloadSettings::changeFolder() {
38
39 #ifdef APP_MAC
40     QFileDialog* dialog = new QFileDialog(this);
41     dialog->setFileMode(QFileDialog::Directory);
42     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
43     dialog->setDirectory(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
44     dialog->open(this, SLOT(folderChosen(const QString &)));
45 #else
46     QString folder = QFileDialog::getExistingDirectory(window(), tr("Choose the download location"),
47                                                     QDesktopServices::storageLocation(QDesktopServices::HomeLocation),
48                                                     QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
49     folderChosen(folder);
50 #endif
51 }
52
53 void DownloadSettings::folderChosen(const QString &dir) {
54     if (!dir.isEmpty()) {
55         QSettings settings;
56         settings.setValue("downloadFolder", dir);
57         updateMessage();
58         QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
59         if (mainWindow) {
60             QString status;
61             status = tr("Download location changed.");
62             if (DownloadManager::instance()->activeItems() > 0)
63                 status += " " + tr("Current downloads will still go in the previous location.");
64             mainWindow->statusBar()->showMessage(status);
65         }
66     }
67 }
68
69 void DownloadSettings::updateMessage() {
70     QString path = DownloadManager::instance()->currentDownloadFolder();
71     QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/";
72     QString displayPath = path;
73     displayPath = displayPath.remove(home);
74     message->setText(
75             tr("Downloading to: %1")
76             .arg("<a href='file://%1' style='text-decoration:none; color:palette(text); font-weight:bold'>%2</a>")
77             .arg(path, displayPath));
78 }