summaryrefslogtreecommitdiffstats
path: root/juk/mediafiles.cpp
blob: 7a0eb6a2ca87c03f9d133d3c8b4efe602a5aa5a0 (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
/***************************************************************************
    begin                : Fri Sep 13 2002
    copyright            : (C) 2002 - 2004 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kfiledialog.h>
#include <kdebug.h>
#include <klocale.h>
#include <kio/netaccess.h>

#include "mediafiles.h"

#include <taglib/tag.h>
#if (TAGLIB_MAJOR_VERSION>1) ||  \
   ((TAGLIB_MAJOR_VERSION==1) && (TAGLIB_MINOR_VERSION>=2))
#define TAGLIB_1_2
#endif
#if (TAGLIB_MAJOR_VERSION>1) ||  \
   ((TAGLIB_MAJOR_VERSION==1) && (TAGLIB_MINOR_VERSION>=3))
#define TAGLIB_1_3
#endif

namespace MediaFiles {
    TQStringList mimeTypes();

    static const char mp3Type[]  = "audio/mpeg";
    static const char oggType[]  = "application/ogg";
    static const char flacType[] = "audio/x-flac";
    static const char mpcType[]  = "audio/x-musepack";
    static const char m3uType[]  = "audio/mpegurl";

    static const char vorbisType[]  = "audio/x-vorbis";
    static const char oggflacType[] = "audio/x-oggflac";

    static const char playlistExtension[] = ".m3u";
}

TQStringList MediaFiles::openDialog(TQWidget *tqparent)
{
    KFileDialog dialog(TQString(), TQString(), tqparent, "filedialog", true);
    dialog.setOperationMode(KFileDialog::Opening);

    dialog.setCaption(i18n("Open"));
    dialog.setMode(KFile::Files | KFile::LocalOnly);
    // dialog.ops->clearHistory();
    dialog.setMimeFilter(mimeTypes());

    dialog.exec();

    return convertURLsToLocal(dialog.selectedFiles());
}

TQString MediaFiles::savePlaylistDialog(const TQString &playlistName, TQWidget *tqparent)
{
    TQString fileName = KFileDialog::getSaveFileName(playlistName + playlistExtension,
                                                    TQString("*").append(playlistExtension),
                                                    tqparent,
                                                    i18n("Playlists"));
    if(!fileName.isEmpty() && !fileName.endsWith(playlistExtension))
       fileName.append(playlistExtension);

    return fileName;
}

bool MediaFiles::isMediaFile(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);

    return result->is(mp3Type) || result->is(oggType) || result->is(flacType)
#ifdef TAGLIB_1_3
                               || result->is(mpcType)
#endif
    ;
}

bool MediaFiles::isPlaylistFile(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);
    return result->is(m3uType);
}

bool MediaFiles::isMP3(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);
    return result->is(mp3Type);
}

bool MediaFiles::isOgg(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);
    return result->is(oggType);
}

bool MediaFiles::isFLAC(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);
    return result->is(flacType);
}

bool MediaFiles::isMPC(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, true);
    return result->is(mpcType);
}

bool MediaFiles::isVorbis(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, false);
    return result->is(vorbisType);
}

bool MediaFiles::isOggFLAC(const TQString &fileName)
{
    KMimeType::Ptr result = KMimeType::findByPath(fileName, 0, false);
    return result->is(oggflacType);
}

TQStringList MediaFiles::mimeTypes()
{
    TQStringList l;

    l << mp3Type << oggType << flacType << m3uType << vorbisType
#ifdef TAGLIB_1_2
    << oggflacType
#endif
#ifdef TAGLIB_1_3
    << mpcType
#endif
    ;
    return l;
}

TQStringList MediaFiles::convertURLsToLocal(const TQStringList &urlList, TQWidget *w)
{
    TQStringList result;
    KURL localUrl;

    for(TQStringList::ConstIterator it = urlList.constBegin(); it != urlList.constEnd(); ++it) {
	localUrl = KIO::NetAccess::mostLocalURL(KURL::fromPathOrURL(*it), w);

	if(!localUrl.isLocalFile())
	    kdDebug(65432) << localUrl << " is not a local file, skipping.\n";
	else
	    result.append(localUrl.path());
    }

    return result;
}