]> git.sur5r.net Git - minitube/blob - src/snapshotsettings.cpp
Imported Upstream version 2.4
[minitube] / src / snapshotsettings.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 "snapshotsettings.h"
22 #include "mainwindow.h"
23 #include <QDesktopServices>
24 #ifdef APP_MAC
25 #include "macutils.h"
26 #endif
27 #include "constants.h"
28
29 SnapshotSettings::SnapshotSettings(QWidget *parent) : QWidget(parent) {
30     QBoxLayout *layout = new QHBoxLayout(this);
31     layout->setContentsMargins(5, 0, 10, 0);
32     layout->setSpacing(5);
33
34     thumb = new QToolButton();
35     thumb->setStyleSheet("border:0");
36     thumb->setCursor(Qt::PointingHandCursor);
37     connect(thumb, SIGNAL(clicked()), SLOT(openFile()));
38     layout->addWidget(thumb);
39
40     message = new QLabel();
41     connect(message, SIGNAL(linkActivated(QString)), this, SLOT(showFile()));
42     layout->addWidget(message);
43
44     changeFolderButton = new QPushButton();
45     changeFolderButton->setAttribute(Qt::WA_MacMiniSize);
46     changeFolderButton->setText(tr("Change location..."));
47     connect(changeFolderButton, SIGNAL(clicked()), SLOT(changeFolder()));
48     layout->addWidget(changeFolderButton);
49 }
50
51 void SnapshotSettings::setSnapshot(const QPixmap &pixmap, const QString &filename) {
52     QPixmap p = pixmap.scaledToHeight(message->sizeHint().height());
53     QIcon icon(p);
54     thumb->setIcon(icon);
55     thumb->setIconSize(p.size());
56     // thumb->setPixmap(pixmap.scaledToHeight(message->sizeHint().height()));
57
58     this->filename = filename;
59     QFileInfo fileInfo(filename);
60     QString path = fileInfo.absolutePath();
61     QString display = displayPath(path);
62
63     QString msg = tr("Snapshot saved to %1")
64             .arg("<a href='showFile' style='text-decoration:none; color:palette(text); font-weight:bold'>%1</a>")
65             .arg(display);
66     message->setText(msg);
67 }
68
69 void SnapshotSettings::setCurrentLocation(const QString &location) {
70     QSettings settings;
71     settings.setValue("snapshotsFolder", location);
72 }
73
74 QString SnapshotSettings::getCurrentLocation() {
75     QSettings settings;
76     QString location = settings.value("snapshotsFolder").toString();
77     if (location.isEmpty() || !QFile::exists(location)) {
78 #if QT_VERSION >= 0x050000
79         location = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
80 #else
81         location = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
82 #endif
83 #ifdef APP_MAC_STORE
84         location += "/MinitubeforYouTube";
85 #endif
86     }
87     return location;
88 }
89
90 QString SnapshotSettings::displayPath(const QString &path) {
91 #ifdef APP_MAC
92     return QDir(path).dirName();
93 #endif
94
95 #if QT_VERSION >= 0x050000
96     QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
97 #else
98     QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
99 #endif
100     QString displayPath = path;
101     displayPath = displayPath.remove(home + "/");
102     return displayPath;
103 }
104
105 void SnapshotSettings::changeFolder() {
106     QString path;
107 #ifdef APP_MAC
108     QFileDialog* dialog = new QFileDialog(this);
109     dialog->setFileMode(QFileDialog::Directory);
110     dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
111 #if QT_VERSION >= 0x050000
112     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
113 #else
114     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
115 #endif
116     dialog->setDirectory(path);
117     dialog->open(this, SLOT(folderChosen(const QString &)));
118 #else
119
120 #if QT_VERSION >= 0x050000
121     path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
122 #else
123     path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
124 #endif
125     QString folder = QFileDialog::getExistingDirectory(window(), QString(),
126                                                        path,
127                                                        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);
128     folderChosen(folder);
129 #endif
130 }
131
132 void SnapshotSettings::folderChosen(const QString &dir) {
133     if (!dir.isEmpty()) {
134         setCurrentLocation(dir);
135         QString status = tr("Snapshots location changed.");
136         MainWindow::instance()->showMessage(status);
137     }
138 }
139
140 void SnapshotSettings::showFile() {
141     QFileInfo info(filename);
142 #ifdef APP_MAC
143     mac::showInFinder(info.absoluteFilePath());
144 #else
145     QUrl url = QUrl::fromLocalFile(info.absolutePath());
146     QDesktopServices::openUrl(url);
147 #endif
148 }
149
150 void SnapshotSettings::openFile() {
151     QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
152 }