[112] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[188] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[112] | 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"
|
---|
[181] | 21 | #include "mediadata.h"
|
---|
[128] | 22 | #include "filehash.h" // hash function
|
---|
[112] | 23 | #include <QSettings>
|
---|
| 24 | #include <QFile>
|
---|
| 25 | #include <QDir>
|
---|
[181] | 26 | #include <QDebug>
|
---|
[112] | 27 |
|
---|
| 28 | FileSettingsHash::FileSettingsHash(QString directory) : FileSettingsBase(directory)
|
---|
| 29 | {
|
---|
| 30 | base_dir = directory + "/file_settings";
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | FileSettingsHash::~FileSettingsHash() {
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 |
|
---|
[181] | 37 | QString FileSettingsHash::configFile(const QString & filename, int type, QString * output_dir) {
|
---|
[112] | 38 | QString res;
|
---|
| 39 |
|
---|
[181] | 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 |
|
---|
[112] | 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 |
|
---|
[181] | 59 | bool FileSettingsHash::existSettingsFor(QString filename, int type) {
|
---|
| 60 | qDebug() << "FileSettingsHash::existSettingsFor:" << filename;
|
---|
[112] | 61 |
|
---|
[181] | 62 | if (type != TYPE_FILE && type != TYPE_STREAM) return false;
|
---|
[112] | 63 |
|
---|
[181] | 64 | QString config_file = configFile(filename, type);
|
---|
[112] | 65 |
|
---|
[181] | 66 | qDebug() << "FileSettingsHash::existSettingsFor: config_file:" << config_file;
|
---|
| 67 |
|
---|
[112] | 68 | return QFile::exists(config_file);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[181] | 71 | void FileSettingsHash::loadSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
|
---|
| 72 | qDebug() << "FileSettings::loadSettingsFor:" << filename << "type:" << type;
|
---|
[112] | 73 |
|
---|
[181] | 74 | if (type != TYPE_FILE && type != TYPE_STREAM) return;
|
---|
[112] | 75 |
|
---|
[181] | 76 | QString config_file = configFile(filename, type);
|
---|
[112] | 77 |
|
---|
[181] | 78 | qDebug() << "FileSettingsHash::loadSettingsFor: config_file:" << config_file;
|
---|
| 79 |
|
---|
[112] | 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");
|
---|
[176] | 86 | mset.load(&settings, player);
|
---|
[112] | 87 | settings.endGroup();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[181] | 91 | void FileSettingsHash::saveSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
|
---|
| 92 | qDebug() << "FileSettingsHash::saveSettingsFor:" << filename << "type:" << type;
|
---|
[112] | 93 |
|
---|
[181] | 94 | if (type != TYPE_FILE && type != TYPE_STREAM) return;
|
---|
| 95 |
|
---|
[112] | 96 | QString output_dir;
|
---|
[181] | 97 | QString config_file = configFile(filename, type, &output_dir);
|
---|
[112] | 98 |
|
---|
[181] | 99 | qDebug() << "FileSettingsHash::saveSettingsFor: config_file:" << config_file;
|
---|
| 100 | qDebug() << "FileSettingsHash::saveSettingsFor: output_dir:" << output_dir;
|
---|
[112] | 101 |
|
---|
| 102 | if (!config_file.isEmpty()) {
|
---|
| 103 | QDir d(base_dir);
|
---|
| 104 | if (!d.exists(output_dir)) {
|
---|
| 105 | if (!d.mkpath(output_dir)) {
|
---|
[181] | 106 | qWarning() << "FileSettingsHash::saveSettingsFor: can't create directory" << QString(base_dir + "/" + output_dir);
|
---|
[112] | 107 | return;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | QSettings settings(config_file, QSettings::IniFormat);
|
---|
| 112 |
|
---|
| 113 | /* settings.setValue("filename", filename); */
|
---|
| 114 |
|
---|
| 115 | settings.beginGroup("file_settings");
|
---|
[176] | 116 | mset.save(&settings, player);
|
---|
[112] | 117 | settings.endGroup();
|
---|
| 118 | settings.sync();
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|