]> git.sur5r.net Git - minitube/blob - src/downloadsettings.cpp
Imported Upstream version 2.4
[minitube] / src / downloadsettings.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "downloadsettings.h"
22 #include "downloadmanager.h"
23 #include "mainwindow.h"
24
25 DownloadSettings::DownloadSettings(QWidget *parent) : QWidget(parent) {
26
27     QBoxLayout *layout = new QHBoxLayout(this);
28     layout->setMargin(10);
29
30     message = new QLabel(this);
31     message->setOpenExternalLinks(true);
32     layout->addWidget(message);
33
34     changeFolderButton = new QPushButton(this);
35     changeFolderButton->setText(tr("Change location..."));
36     changeFolderButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
37     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
38     layout->addWidget(changeFolderButton);
39
40     updateMessage();
41 }
42
43 void DownloadSettings::paintEvent(QPaintEvent * /*event*/) {
44     QPainter painter(this);
45 #ifdef APP_MAC
46     QBrush brush;
47     if (window()->isActiveWindow()) {
48         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
49     } else {
50         brush = palette().window();
51     }
52     painter.fillRect(0, 0, width(), height(), brush);
53 #endif
54     painter.setPen(palette().color(QPalette::Mid));
55     painter.drawLine(0, 0, width(), 0);
56 }
57
58 void DownloadSettings::changeFolder() {
59     QString path;
60 #ifdef APP_MAC
61     QFileDialog* dialog = new QFileDialog(this);
62     dialog->setFileMode(QFileDialog::Directory);
63     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
64 #if QT_VERSION >= 0x050000
65     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
66 #else
67     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
68 #endif
69     dialog->setDirectory(path);
70     dialog->open(this, SLOT(folderChosen(const QString &)));
71 #else
72
73 #if QT_VERSION >= 0x050000
74     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
75 #else
76     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
77 #endif
78     QString folder = QFileDialog::getExistingDirectory(window(), tr("Choose the download location"),
79                                                        path,
80                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
81     folderChosen(folder);
82 #endif
83 }
84
85 void DownloadSettings::folderChosen(const QString &dir) {
86     if (!dir.isEmpty()) {
87         QSettings settings;
88         settings.setValue("downloadFolder", dir);
89         updateMessage();
90         QString status = tr("Download location changed.");
91         if (DownloadManager::instance()->activeItems() > 0)
92             status += " " + tr("Current downloads will still go in the previous location.");
93         MainWindow::instance()->showMessage(status);
94     }
95 }
96
97 void DownloadSettings::updateMessage() {
98     QString path = DownloadManager::instance()->currentDownloadFolder();
99 #if QT_VERSION >= 0x050000
100     QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
101 #else
102     QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
103 #endif
104     QString displayPath = path;
105     displayPath = displayPath.remove(home + "/");
106     message->setText(
107                 tr("Downloading to: %1")
108                 .arg("<a href='file://%1' style='text-decoration:none; color:palette(text); font-weight:bold'>%2</a>")
109                 .arg(path, displayPath));
110 }