summaryrefslogtreecommitdiffstats
path: root/noatun/modules/metatag/metatag.cpp
blob: 8fde5123a12dac58e3261f7742d76a8c4a794a22 (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

#include "metatag.h"
#include "edit.h"

#include <string.h>

#include <noatun/app.h>
#include <noatun/stdaction.h>

#include <tqfile.h>
#include <tqlabel.h>
#include <layout.h>
#include <tqregexp.h>

#include <klocale.h>
#include <kaction.h>
#include <kglobal.h>
#include <klineedit.h>
#include <kconfig.h>
#include <kaction.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <kfilemetainfo.h>

extern "C"
{
	KDE_EXPORT Plugin *create_plugin()
	{
		return new MetaTagLoader;
	}
}

MetaTagLoader::MetaTagLoader():Plugin()
{
	mAction = new KAction(i18n("&Tag Editor..."), "edit", 0, this, TQT_SLOT(editTag()), this, "edittag");
	napp->pluginActionMenu()->insert(mAction);
}

MetaTagLoader::~MetaTagLoader()
{
	napp->pluginActionMenu()->remove(mAction);
}

void MetaTagLoader::editTag()
{
	PlaylistItem i = napp->player()->current();

	if(!i)
		return;

	Editor *e = new Editor();
	e->open(i);
	e->show();

	connect(e, TQT_SIGNAL(saved(PlaylistItem &)),
		TQT_SLOT(update(PlaylistItem &)));
}

bool MetaTagLoader::update(PlaylistItem & item)
{
	KFileMetaInfo file_info(item.file(),item.mimetype());

	// Ack, no file info :(
	if ( !file_info.isValid() )
		return false;

	if(item.length() == -1) // no value set, set almost correct id3tag time
	{
		KFileMetaInfoItem length_item = file_info.item("Length");
		if(length_item.isValid())
		{
			int numVal = length_item.value().toInt();
			if (numVal != 0)
				item.setLength(numVal * 1000);
		}
	}

	// Yes, this is icky. It maps KFileMetaInfo property names to Noatun's
	setProperty(file_info, item, "Title", "title");
	setProperty(file_info, item, "Artist", "author");
	setProperty(file_info, item, "Album", "album");
	setProperty(file_info, item, "Genre", "genre");
	setProperty(file_info, item, "Tracknumber", "track");
	setProperty(file_info, item, "Date", "date");
	setProperty(file_info, item, "Comment", "comment");
	setProperty(file_info, item, "Location", "location");
	setProperty(file_info, item, "Organization", "organization");

	// Now map the audio properties over
	setProperty(file_info, item, "Bitrate", "bitrate");
	setProperty(file_info, item, "Sample Rate", "samplerate");
	setProperty(file_info, item, "Channels", "channels");

	return true;
}

bool MetaTagLoader::setProperty(KFileMetaInfo &info, PlaylistItem &item, const TQString &key, const TQString &property)
{
	KFileMetaInfoItem info_item = info.item(key);

	if ( info_item.isValid() )
	{
		if (!info_item.value().toString().stripWhiteSpace().isEmpty())
		{
			// The item is valid and non-empty, add it
			item.setProperty(property, info_item.value().toString());
		}
		else
		{
			// If the info_item is valid, but empty.
			// This means we know for a fact that this
			// property has no value. Blow it away.
			item.clearProperty(property);
		}
		return true;
	}

	// The item isn't valid, so we don't know that it has
	// no value. Don't remove the property, so we can work
	// well with other tag readers, like Lucky
	return false;
}

#include "metatag.moc"