OSDN Git Service

Happy new year 2014!
[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 RecentlyUsed::RecentlyUsed(void)
47 {
48         initRecentlyUsed(this);
49 }
50
51 RecentlyUsed::~RecentlyUsed(void)
52 {
53         /*nothing to do*/
54 }
55
56 void RecentlyUsed::initRecentlyUsed(RecentlyUsed *recentlyUsed)
57 {
58         recentlyUsed->m_sourceDirectory = QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
59         recentlyUsed->m_outputDirectory = QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
60         recentlyUsed->m_filterIndex = 0;
61         recentlyUsed->m_lastUpdateCheck = QDate(1969, 8, 15).toJulianDay();
62 }
63
64 void RecentlyUsed::loadRecentlyUsed(RecentlyUsed *recentlyUsed)
65 {
66         RecentlyUsed defaults;
67         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
68         int temp = 0;
69
70         recentlyUsed->m_sourceDirectory = settings.value(KEY_SOURCE_DIR, defaults.m_sourceDirectory).toString();
71         recentlyUsed->m_outputDirectory = settings.value(KEY_OUTPUT_DIR, defaults.m_outputDirectory).toString();
72         READ_INT(settings, KEY_FILTER_IDX, defaults.m_filterIndex,     &recentlyUsed->m_filterIndex);
73         READ_INT(settings, KEY_UPDATE_CHK, defaults.m_lastUpdateCheck, &recentlyUsed->m_lastUpdateCheck);
74
75         if(!VALID_DIR(recentlyUsed->m_sourceDirectory)) recentlyUsed->m_sourceDirectory = defaults.m_sourceDirectory;
76         if(!VALID_DIR(recentlyUsed->m_outputDirectory)) recentlyUsed->m_outputDirectory = defaults.m_outputDirectory;
77         recentlyUsed->m_filterIndex = qBound(0, recentlyUsed->m_filterIndex, int(ARRAY_SIZE(X264_FILE_TYPE_FILTERS)-1));
78 }
79
80 void RecentlyUsed::saveRecentlyUsed(RecentlyUsed *recentlyUsed)
81 {
82         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
83         if(settings.isWritable())
84         {
85                 settings.setValue(KEY_SOURCE_DIR, recentlyUsed->m_sourceDirectory);
86                 settings.setValue(KEY_OUTPUT_DIR, recentlyUsed->m_outputDirectory);
87                 settings.setValue(KEY_FILTER_IDX, recentlyUsed->m_filterIndex);
88                 settings.setValue(KEY_UPDATE_CHK, recentlyUsed->m_lastUpdateCheck);
89                 settings.sync();
90         }
91         else
92         {
93                 qWarning("Settings are not writable!");
94         }
95 }