OSDN Git Service

Code refactoring: Now "Preferences" and "Recently" used models are in separate classe...
[x264-launcher/x264-launcher.git] / src / model_recently.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 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
30 #define ARRAY_SIZE(ARRAY) (sizeof((ARRAY))/sizeof((ARRAY[0])))
31 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
32
33 static const char *KEY_FILTER_IDX = "path/filterIndex";
34 static const char *KEY_SOURCE_DIR = "path/directory_openFrom";
35 static const char *KEY_OUTPUT_DIR = "path/directory_saveTo";
36
37 RecentlyUsed::RecentlyUsed(void)
38 {
39         initRecentlyUsed(this);
40 }
41
42 void RecentlyUsed::initRecentlyUsed(RecentlyUsed *recentlyUsed)
43 {
44         recentlyUsed->m_sourceDirectory = QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
45         recentlyUsed->m_outputDirectory = QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
46         recentlyUsed->m_filterIndex = 0;
47 }
48
49 void RecentlyUsed::loadRecentlyUsed(RecentlyUsed *recentlyUsed)
50 {
51         RecentlyUsed defaults;
52         
53         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
54         recentlyUsed->m_sourceDirectory = settings.value(KEY_SOURCE_DIR, defaults.m_sourceDirectory).toString();
55         recentlyUsed->m_outputDirectory = settings.value(KEY_OUTPUT_DIR, defaults.m_outputDirectory).toString();
56         recentlyUsed->m_filterIndex = settings.value(KEY_FILTER_IDX, defaults.m_filterIndex).toInt();
57
58         if(!VALID_DIR(recentlyUsed->m_sourceDirectory)) recentlyUsed->m_sourceDirectory = defaults.m_sourceDirectory;
59         if(!VALID_DIR(recentlyUsed->m_outputDirectory)) recentlyUsed->m_outputDirectory = defaults.m_outputDirectory;
60         recentlyUsed->m_filterIndex = qBound(0, recentlyUsed->m_filterIndex, int(ARRAY_SIZE(X264_FILE_TYPE_FILTERS)-1));
61 }
62
63 void RecentlyUsed::saveRecentlyUsed(RecentlyUsed *recentlyUsed)
64 {
65         QSettings settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat);
66         if(settings.isWritable())
67         {
68                 settings.setValue(KEY_SOURCE_DIR, recentlyUsed->m_sourceDirectory);
69                 settings.setValue(KEY_OUTPUT_DIR, recentlyUsed->m_outputDirectory);
70                 settings.setValue(KEY_FILTER_IDX, recentlyUsed->m_filterIndex);
71                 settings.sync();
72         }
73 }