summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/file.cpp
blob: b478e0fdae732ce74bd03e8b44d4bee799583de6 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2003-2004 Charles Samuels <charles@kde.org>
// See the file COPYING for redistribution terms.

#include "file.h"
#include "selector.h"
#include "query.h"

#include <iostream>

#include <klocale.h>
#include <kfilemetainfo.h>
#include <kmimetype.h>


File::File(Base *base, FileId id)
{
	mBase = base;
	mId = id;

}

File::File(const File &ref)
{
	operator =(ref);
}

File::File()
{
	mBase=0;
	mId = 0;
}

File &File::operator=(const File &ref)
{
	mBase = ref.mBase;
	mId = ref.mId;

	return *this;
}

QString File::file() const
{
	return property("file");
}

KURL File::url() const
{
	KURL url;
	url.setPath(file());
	return url;
}

struct Map { const char *kfmi; const char *noatun; };
static const Map propertyMap[] =
{
	{ "Title", "ob::title_" },
	{ "Artist", "ob::author_" },
	{ "Album", "ob::album_" },
	{ "Genre", "ob::genre_" },
	{ "Tracknumber", "ob::track_" },
	{ "Date", "ob::date_" },
	{ "Comment", "ob::comment_" },
	{ "Location", "ob::location_" },
	{ "Organization", "ob::organization_" },
	{ "Bitrate", "ob::bitrate_" },
	{ "Sample Rate", "ob::samplerate_" },
	{ "Channels", "ob::channels_" },
	{ 0, 0 }
};

QString File::property(const QString &property) const
{
	QString str = base()->property(id(), property);

	if (!str)
	{
		QString mangled = "ob::" + property + "_";
		str = base()->property(id(), mangled);
	}

	return str;
}


void File::makeCache()
{
	setProperty("ob::mimetype_", KMimeType::findByPath(file())->name());
	KFileMetaInfo info(file());

	for (int i=0; propertyMap[i].kfmi; i++)
	{
		QString kname(propertyMap[i].kfmi);
		if (info.isValid() && kname.length())
		{
			QString val = info.item(kname).string(false);
			if (val=="---" || !val.stripWhiteSpace().length())
			{ // grr
				val = "";
			}
			if (val.length())
			{
				setProperty(propertyMap[i].noatun, val);
			}
		}
	}
}


void File::setProperty(const QString &key, const QString &value)
{
	if (property(key) == value) return;
	base()->setProperty(id(), key, value);
	PlaylistItem p=new Item(*this);
	p.data()->modified();
}

void File::clearProperty(const QString &key)
{
	if (property(key).isNull()) return;
	base()->clearProperty(id(), key);
	PlaylistItem p=new Item(*this);
	p.data()->modified();
}

QStringList File::properties() const
{
	QStringList l = base()->properties(id());

	for (int i=0; propertyMap[i].noatun; i++)
	{
		if (property(propertyMap[i].noatun).length())
		{
			l += propertyMap[i].noatun;
		}
	}
	return l;
}

void File::setId(FileId id)
{
	base()->move(mId, id);
	mId = id;
}


void File::setPosition(Query *query, const File &after)
{
	setProperty(
			"Oblique:after_" + query->name() + '_',
			QString::number(after.id())
		);
}

bool File::getPosition(const Query *query, File *after) const
{
	assert(query);
	assert(after);
	QString name = "Oblique:after_" + query->name() + '_';
	if (name.isEmpty()) return false;
	
	QString val = property(name);
	if (val.isEmpty())
		return false;
	*after = File(mBase, val.toUInt());
	return true;
}


void File::remove()
{
	PlaylistItem p=new Item(*this);
	p.data()->removed();
	mBase->remove(*this);
}

void File::addTo(Slice *slice)
{
	QString slices = property("Oblique:slices_");
	slices += "\n" + QString::number(slice->id(), 16);
	setProperty("Oblique:slices_", slices);
	emit mBase->addedTo(slice, *this);
}

void File::removeFrom(Slice *slice)
{
	QString slices = property("Oblique:slices_");
	QStringList sliceList = QStringList::split('\n', slices);
	QString sid = QString::number(slice->id(), 16);
	sliceList.remove(sid);

	slices = sliceList.join("\n");
	setProperty("Oblique:slices_", slices);
	emit mBase->removedFrom(slice, *this);
}

bool File::isIn(const Slice *slice) const
{
	int id = slice->id();
	if (id==0) return true;

	QString slices = property("Oblique:slices_");
	QStringList sliceList = QStringList::split('\n', slices);
	for (QStringList::Iterator i(sliceList.begin()); i!= sliceList.end(); ++i)
	{
		if ((*i).toInt(0, 16) == id) return true;
	}
	return false;
}



Slice::Slice(Base *base, int id, const QString &name)
{
	mId = id;
	mBase = base;
	mName = name;
}

QString Slice::name() const
{
	if (mId == 0) return i18n("Complete Collection");
	return mName;
}

void Slice::setName(const QString &name)
{
	if (mId == 0) return;
	mName = name;
	emit mBase->slicesModified();
}

void Slice::remove()
{
	if (mId == 0) return;
	mBase->removeSlice(this);
	Base *base = mBase;

	for (FileId fi=1; ; fi++)
	{
		File f = base->first(fi);
		f.removeFrom(this);
		fi = f.id();
	}
	emit base->slicesModified();
}