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