1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "filesettingshash.h"
|
---|
20 | #include "mediasettings.h"
|
---|
21 | #include "mediadata.h"
|
---|
22 | #include "filehash.h" // hash function
|
---|
23 | #include <QSettings>
|
---|
24 | #include <QFile>
|
---|
25 | #include <QDir>
|
---|
26 | #include <QDebug>
|
---|
27 |
|
---|
28 | FileSettingsHash::FileSettingsHash(QString directory) : FileSettingsBase(directory)
|
---|
29 | {
|
---|
30 | base_dir = directory + "/file_settings";
|
---|
31 | }
|
---|
32 |
|
---|
33 | FileSettingsHash::~FileSettingsHash() {
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | QString FileSettingsHash::configFile(const QString & filename, int type, QString * output_dir) {
|
---|
38 | QString res;
|
---|
39 |
|
---|
40 | QString hash;
|
---|
41 | if (type == TYPE_FILE) {
|
---|
42 | hash = FileHash::calculateHash(filename);
|
---|
43 | } else {
|
---|
44 | QByteArray ba;
|
---|
45 | for (int n = filename.count()-1; n >= 0; --n) {
|
---|
46 | ba += filename.at(n);
|
---|
47 | }
|
---|
48 | //qDebug() << "FileSettingsHash::configFile: ba:" << ba;
|
---|
49 | hash = ba.toBase64();
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (!hash.isEmpty()) {
|
---|
53 | if (output_dir != 0) (*output_dir) = hash[0];
|
---|
54 | res = base_dir +"/"+ hash[0] +"/"+ hash + ".ini";
|
---|
55 | }
|
---|
56 | return res;
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool FileSettingsHash::existSettingsFor(QString filename, int type) {
|
---|
60 | qDebug() << "FileSettingsHash::existSettingsFor:" << filename;
|
---|
61 |
|
---|
62 | if (type != TYPE_FILE && type != TYPE_STREAM) return false;
|
---|
63 |
|
---|
64 | QString config_file = configFile(filename, type);
|
---|
65 |
|
---|
66 | qDebug() << "FileSettingsHash::existSettingsFor: config_file:" << config_file;
|
---|
67 |
|
---|
68 | return QFile::exists(config_file);
|
---|
69 | }
|
---|
70 |
|
---|
71 | void FileSettingsHash::loadSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
|
---|
72 | qDebug() << "FileSettings::loadSettingsFor:" << filename << "type:" << type;
|
---|
73 |
|
---|
74 | if (type != TYPE_FILE && type != TYPE_STREAM) return;
|
---|
75 |
|
---|
76 | QString config_file = configFile(filename, type);
|
---|
77 |
|
---|
78 | qDebug() << "FileSettingsHash::loadSettingsFor: config_file:" << config_file;
|
---|
79 |
|
---|
80 | mset.reset();
|
---|
81 |
|
---|
82 | if ((!config_file.isEmpty()) && (QFile::exists(config_file))) {
|
---|
83 | QSettings settings(config_file, QSettings::IniFormat);
|
---|
84 |
|
---|
85 | settings.beginGroup("file_settings");
|
---|
86 | mset.load(&settings, player);
|
---|
87 | settings.endGroup();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | void FileSettingsHash::saveSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
|
---|
92 | qDebug() << "FileSettingsHash::saveSettingsFor:" << filename << "type:" << type;
|
---|
93 |
|
---|
94 | if (type != TYPE_FILE && type != TYPE_STREAM) return;
|
---|
95 |
|
---|
96 | QString output_dir;
|
---|
97 | QString config_file = configFile(filename, type, &output_dir);
|
---|
98 |
|
---|
99 | qDebug() << "FileSettingsHash::saveSettingsFor: config_file:" << config_file;
|
---|
100 | qDebug() << "FileSettingsHash::saveSettingsFor: output_dir:" << output_dir;
|
---|
101 |
|
---|
102 | if (!config_file.isEmpty()) {
|
---|
103 | QDir d(base_dir);
|
---|
104 | if (!d.exists(output_dir)) {
|
---|
105 | if (!d.mkpath(output_dir)) {
|
---|
106 | qWarning() << "FileSettingsHash::saveSettingsFor: can't create directory" << QString(base_dir + "/" + output_dir);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | QSettings settings(config_file, QSettings::IniFormat);
|
---|
112 |
|
---|
113 | /* settings.setValue("filename", filename); */
|
---|
114 |
|
---|
115 | settings.beginGroup("file_settings");
|
---|
116 | mset.save(&settings, player);
|
---|
117 | settings.endGroup();
|
---|
118 | settings.sync();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|