1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "model_recently.h"
26 #include <QDesktopServices>
31 #define ARRAY_SIZE(ARRAY) (sizeof((ARRAY))/sizeof((ARRAY[0])))
32 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
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";
39 static void READ_INT(QSettings
&settings
, const QString
&key
, int default, int *value
)
42 const int temp
= settings
.value(key
, default).toInt(&ok
);
43 *value
= (ok
) ? temp
: default;
46 static QString
moviesLocation(void)
48 static const QDesktopServices::StandardLocation locations
[3] =
50 QDesktopServices::MoviesLocation
, QDesktopServices::DesktopLocation
, QDesktopServices::HomeLocation
53 for(size_t i
= 0; i
< 3; i
++)
55 const QString path
= QDir::fromNativeSeparators(QDesktopServices::storageLocation(locations
[i
]));
59 if(!directory
.exists())
61 if(!directory
.mkpath("."))
66 return directory
.absolutePath();
70 return QDir::rootPath();
73 RecentlyUsed::RecentlyUsed(void)
75 initRecentlyUsed(this);
78 RecentlyUsed::~RecentlyUsed(void)
83 void RecentlyUsed::initRecentlyUsed(RecentlyUsed
*recentlyUsed
)
85 recentlyUsed
->m_sourceDirectory
= moviesLocation();
86 recentlyUsed
->m_outputDirectory
= moviesLocation();
87 recentlyUsed
->m_filterIndex
= 0;
88 recentlyUsed
->m_lastUpdateCheck
= QDate(1969, 8, 15).toJulianDay();
91 void RecentlyUsed::loadRecentlyUsed(RecentlyUsed
*recentlyUsed
)
93 RecentlyUsed defaults
;
94 QSettings
settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat
);
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
);
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));
107 void RecentlyUsed::saveRecentlyUsed(RecentlyUsed
*recentlyUsed
)
109 QSettings
settings(QString("%1/last.ini").arg(x264_data_path()), QSettings::IniFormat
);
110 if(settings
.isWritable())
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
);
120 qWarning("Settings are not writable!");