[Feature] Improve uri handling.
[Khopper.git] / plugins / cuesheet / cuesheetplugin.cpp
blob5964a0e3d5ecf5cf4b07b9fc1b7dfdf2f19e39aa
1 /**
2 * @file cuesheetplugin.cpp
3 * @author Wei-Cheng Pan
5 * Copyright (C) 2008 Wei-Cheng Pan <legnaleurc@gmail.com>
7 * This file is part of Khopper.
9 * Khopper is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * Khopper is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "cuesheetplugin.hpp"
23 #include "codecselector.hpp"
24 #include "cuesheetparser.hpp"
26 #include "khopper/playlist.hpp"
27 #include "khopper/text.hpp"
29 #include <QtCore/QDir>
30 #include <QtCore/QtPlugin>
32 Q_EXPORT_PLUGIN2( KHOPPER_PLUGIN_ID, khopper::plugin::CueSheetPlugin )
34 using namespace khopper::plugin;
35 using khopper::album::PlayList;
37 CueSheetPlugin::CueSheetPlugin():
38 AbstractPlugin() {
39 this->setID( KHOPPER_STRINGIZE(KHOPPER_PLUGIN_ID) );
40 this->setVersion( KHOPPER_STRINGIZE(KHOPPER_VERSION) );
43 void CueSheetPlugin::doInstall() {
44 registerPlayList( this->getID(), []( const QUrl & uri )->unsigned int {
45 if( uri.scheme() != "file" ) {
46 // TODO: network support
47 return 0U;
49 QFileInfo info( uri.toLocalFile() );
50 if( info.suffix().toLower() == "cue" ) {
51 return 100U;
52 } else if( info.dir().exists( info.baseName() + ".cue" ) ) {
53 return 200U;
54 } else {
55 return 0U;
57 }, []( const QUrl & uri )->PlayList {
58 QFileInfo info( uri.toLocalFile() );
59 QFile fin;
60 if( info.suffix().toLower() == "cue" ) {
61 fin.setFileName( info.filePath() );
62 } else {
63 fin.setFileName( info.path() + "/" + info.baseName() + ".cue" );
65 fin.open( QIODevice::ReadOnly );
66 QString content = khopper::widget::CodecSelector::selectTextCodec( fin.readAll() );
67 fin.close();
68 return khopper::album::CueSheetParser::load( content, info.dir() );
69 } );
72 void CueSheetPlugin::doUninstall() {
73 unregisterPlayList( this->getID() );