OSDN Git Service

On systems where the QDesktopServices::MoviesLocation cannot be determined, a bad...
[x264-launcher/x264-launcher.git] / src / model_recently.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "model_recently.h"
23
24 #include "global.h"
25
26 #include <QDesktopServices>
27 #include <QDir>
28 #include <QSettings>
29 #include <QDate>
30
31 #define ARRAY_SIZE(ARRAY) (sizeof((ARRAY))/sizeof((ARRAY[0])))
32 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
33
34 static const char *KEY_FILTER_IDX = "path/filterIndex";
35 static const char *KEY_SOURCE_DIR = "path/directory_openFrom";
36 static const char *KEY_OUTPUT_DIR = "path/directory_saveTo";
37 static const char *KEY_UPDATE_CHK = "auto_update/last_successfull_check";
38
39 static void READ_INT(QSettings &settings, const QString &key, int default, int *value)
40 {
41         bool ok = false;
42         const int temp = settings.value(key, default).toInt(&ok);
43         *value = (ok) ? temp : default;
44 }
45
46 static QString moviesLocation(void)
47 {
48         static const QDesktopServices::StandardLocation locations[3] =
49         {
50                 QDesktopServices::MoviesLocation, QDesktopServices::DesktopLocation, QDesktopServices::HomeLocation
51         };
52
53         for(size_t i = 0; i < 3; i++)
54         {
55                 const QString path = QDir::fromNativeSeparators(QDesktopServices::storageLocation(locations[i]));
56                 if(!path.isEmpty())
57                 {
58                         QDir directory(path);
59                         if(!directory.exists())
60                         {
61                                 if(!directory.mkpath("."))
62                                 {
63                                         continue;
64                                 }
65                         }
66                         return directory.absolutePath();
67                 }
68         }
69
70         return QDir::rootPath();
71 }
72
73 RecentlyUsed::RecentlyUsed(void)
74 {
75         initRecentlyUsed(this);
76 }
77
78 RecentlyUsed::~RecentlyUsed(void)
79 {
80         /*nothing to do*/
81 }
82
83 void RecentlyUsed::initRecentlyUsed(RecentlyUsed *recentlyUsed)
84 {
85         recentlyUsed->m_sourceDirectory = moviesLocation();
86         recentlyUsed->m_outputDirectory = moviesLocation();
87         recentlyUsed->m_filterIndex = 0;
88         recentlyUsed->m_lastUpdateCheck = QDate(1969, 8, 15).toJulianDay();
89 }
90
91 void RecentlyUsed::loadRecentlyUsed(RecentlyUsed *recentlyUsed)
92 {
93         RecentlyUsed defaults;
94         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
95         int temp = 0;
96
97         recentlyUsed->m_sourceDirectory = settings.value(KEY_SOURCE_DIR, defaults.m_sourceDirectory).toString();
98         recentlyUsed->m_outputDirectory = settings.value(KEY_OUTPUT_DIR, defaults.m_outputDirectory).toString();
99         READ_INT(settings, KEY_FILTER_IDX, defaults.m_filterIndex,     &recentlyUsed->m_filterIndex);
100         READ_INT(settings, KEY_UPDATE_CHK, defaults.m_lastUpdateCheck, &recentlyUsed->m_lastUpdateCheck);
101
102         if(!VALID_DIR(recentlyUsed->m_sourceDirectory)) recentlyUsed->m_sourceDirectory = defaults.m_sourceDirectory;
103         if(!VALID_DIR(recentlyUsed->m_outputDirectory)) recentlyUsed->m_outputDirectory = defaults.m_outputDirectory;
104         recentlyUsed->m_filterIndex = qBound(0, recentlyUsed->m_filterIndex, int(ARRAY_SIZE(X264_FILE_TYPE_FILTERS)-1));
105 }
106
107 void RecentlyUsed::saveRecentlyUsed(RecentlyUsed *recentlyUsed)
108 {
109         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
110         if(settings.isWritable())
111         {
112                 settings.setValue(KEY_SOURCE_DIR, recentlyUsed->m_sourceDirectory);
113                 settings.setValue(KEY_OUTPUT_DIR, recentlyUsed->m_outputDirectory);
114                 settings.setValue(KEY_FILTER_IDX, recentlyUsed->m_filterIndex);
115                 settings.setValue(KEY_UPDATE_CHK, recentlyUsed->m_lastUpdateCheck);
116                 settings.sync();
117         }
118         else
119         {
120                 qWarning("Settings are not writable!");
121         }
122 }