summaryrefslogtreecommitdiff
path: root/src/filesettingshash.cpp
blob: 798305c323519990e9151ff363a0f194630832b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "filesettingshash.h"
#include "mediasettings.h"
#include "mediadata.h"
#include "filehash.h" // hash function
#include <QSettings>
#include <QFile>
#include <QDir>
#include <QDebug>

FileSettingsHash::FileSettingsHash(QString directory) : FileSettingsBase(directory) 
{
	base_dir = directory + "/file_settings";
}

FileSettingsHash::~FileSettingsHash() {
}


QString FileSettingsHash::configFile(const QString & filename, int type, QString * output_dir) {
	QString res;

	QString hash;
	if (type == TYPE_FILE) {
		hash = FileHash::calculateHash(filename);
	} else {
		QByteArray ba;
		for (int n = filename.count()-1; n >= 0; --n) {
			ba += filename.at(n);
		}
		//qDebug() << "FileSettingsHash::configFile: ba:" << ba;
		hash = ba.toBase64();
	}

	if (!hash.isEmpty()) {
		if (output_dir != 0) (*output_dir) = hash[0];
		res = base_dir +"/"+ hash[0] +"/"+ hash + ".ini";
	}
	return res;
}

bool FileSettingsHash::existSettingsFor(QString filename, int type) {
	qDebug() << "FileSettingsHash::existSettingsFor:" << filename;

	if (type != TYPE_FILE && type != TYPE_STREAM) return false;

	QString config_file = configFile(filename, type);

	qDebug() << "FileSettingsHash::existSettingsFor: config_file:" << config_file;

	return QFile::exists(config_file);
}

void FileSettingsHash::loadSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
	qDebug() << "FileSettings::loadSettingsFor:" << filename << "type:" << type;

	if (type != TYPE_FILE && type != TYPE_STREAM) return;

	QString config_file = configFile(filename, type);

	qDebug() << "FileSettingsHash::loadSettingsFor: config_file:" << config_file;

	mset.reset();

	if ((!config_file.isEmpty()) && (QFile::exists(config_file))) {
		QSettings settings(config_file, QSettings::IniFormat);

		settings.beginGroup("file_settings");
		mset.load(&settings, player);
		settings.endGroup();
	}
}

void FileSettingsHash::saveSettingsFor(QString filename, int type, MediaSettings & mset, int player) {
	qDebug() << "FileSettingsHash::saveSettingsFor:" << filename << "type:" << type;

	if (type != TYPE_FILE && type != TYPE_STREAM) return;

	QString output_dir;
	QString config_file = configFile(filename, type, &output_dir);

	qDebug() << "FileSettingsHash::saveSettingsFor: config_file:" << config_file;
	qDebug() << "FileSettingsHash::saveSettingsFor: output_dir:" << output_dir;

	if (!config_file.isEmpty()) {
		QDir d(base_dir);
		if (!d.exists(output_dir)) {
			if (!d.mkpath(output_dir)) {
				qWarning() << "FileSettingsHash::saveSettingsFor: can't create directory" << QString(base_dir + "/" + output_dir);
				return;
			}
		}

		QSettings settings(config_file, QSettings::IniFormat);

		/* settings.setValue("filename", filename); */

		settings.beginGroup("file_settings");
		mset.save(&settings, player);
		settings.endGroup();
		settings.sync();
	}
}