]> git.sur5r.net Git - minitube/blob - src/downloadsettings.cpp
Imported Upstream version 2.5.1
[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 #include "compatibility/pathsservice.h"
25
26 DownloadSettings::DownloadSettings(QWidget *parent) : QWidget(parent) {
27
28     QBoxLayout *layout = new QHBoxLayout(this);
29     layout->setMargin(10);
30
31     message = new QLabel(this);
32     message->setOpenExternalLinks(true);
33     layout->addWidget(message);
34
35     changeFolderButton = new QPushButton(this);
36     changeFolderButton->setText(tr("Change location..."));
37     changeFolderButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
38     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
39     layout->addWidget(changeFolderButton);
40
41     updateMessage();
42 }
43
44 void DownloadSettings::paintEvent(QPaintEvent * /*event*/) {
45     QPainter painter(this);
46 #ifdef APP_MAC
47     QBrush brush;
48     if (window()->isActiveWindow()) {
49         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
50     } else {
51         brush = palette().window();
52     }
53     painter.fillRect(0, 0, width(), height(), brush);
54 #endif
55     painter.setPen(palette().color(QPalette::Mid));
56     painter.drawLine(0, 0, width(), 0);
57 }
58
59 void DownloadSettings::changeFolder() {
60     const QString path = Paths::getHomeLocation();
61 #ifdef APP_MAC
62     QFileDialog* dialog = new QFileDialog(this);
63     dialog->setFileMode(QFileDialog::Directory);
64     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
65     dialog->setDirectory(path);
66     dialog->open(this, SLOT(folderChosen(const QString &)));
67 #else
68
69     QString folder = QFileDialog::getExistingDirectory(window(), tr("Choose the download location"),
70                                                        path,
71                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
72     folderChosen(folder);
73 #endif
74 }
75
76 void DownloadSettings::folderChosen(const QString &dir) {
77     if (!dir.isEmpty()) {
78         QSettings settings;
79         settings.setValue("downloadFolder", dir);
80         updateMessage();
81         QString status = tr("Download location changed.");
82         if (DownloadManager::instance()->activeItems() > 0)
83             status += " " + tr("Current downloads will still go in the previous location.");
84         MainWindow::instance()->showMessage(status);
85     }
86 }
87
88 void DownloadSettings::updateMessage() {
89     const QString path = DownloadManager::instance()->currentDownloadFolder();
90     const QString home = Paths::getHomeLocation();
91     QString displayPath = path;
92     displayPath = displayPath.remove(home + "/");
93     message->setText(
94                 tr("Downloading to: %1")
95                 .arg("<a href='file://%1' style='text-decoration:none; color:palette(text); font-weight:bold'>%2</a>")
96                 .arg(path, displayPath));
97 }