Killed file removing heisenbug (somehow).
[amarok_sonynw.git] / src / plugin_amarok_sonynw.cpp
bloba3a4a300193cf1fc1ed0e5f0694b61c3e33704c0
1 /***************************************************************************
2 * Copyright (C) 2008 by RafaƂ Rzepecki *
3 * divided.mind@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #define DEBUG_PREFIX "SonyNWMediaDevice"
23 #include "plugin_amarok_sonynw.h"
24 #include "trackmediaitem.h"
26 #include "rubycpp.h"
28 #include <debug.h>
30 #include <qtextcodec.h>
31 #include <qmutex.h>
33 // we are not reentrant
34 static QMutex mutex;
36 AMAROK_EXPORT_PLUGIN( SonyNWMediaDevice )
38 using namespace Ruby;
39 using namespace SonyNW;
41 template <typename T>
42 static Value v(const T &val) { return Value(val); }
44 template <>
45 static Value v(const QString &str) { return Value(str.utf8()); }
47 SonyNWMediaDevice::SonyNWMediaDevice() : MediaDevice(), m_db( 0 )
49 QMutexLocker lock(&mutex);
50 initialize();
51 require("db");
52 require("audiofile");
53 require("mp3");
54 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
57 SonyNWMediaDevice::~SonyNWMediaDevice()
61 bool SonyNWMediaDevice::isConnected()
63 return m_db != 0;
66 QStringList SonyNWMediaDevice::supportedFiletypes()
68 return QStringList("mp3");
71 MediaItem *SonyNWMediaDevice::trackExists( const MetaBundle& bundle )
73 // FIXME really search
74 return 0;
77 bool SonyNWMediaDevice::lockDevice( bool /*tryOnly*/ )
79 // unsure if that is really possible
80 return true;
83 void SonyNWMediaDevice::unlockDevice()
87 bool SonyNWMediaDevice::openDevice( bool silent )
89 QMutexLocker lock(&mutex);
90 m_db = new Ruby::Value(klass("NetworkWalkman::Db").send("new", v(mountPoint())));
91 if (m_db->isNil()) return false;
93 Ruby::Value songs = m_db->send("songs");
94 for (int i = 0; i < RARRAY(VALUE(songs))->len; i++) {
95 Ruby::Value song = rb_ary_entry(songs, i);
96 if (VALUE(m_db->const_send("really_exists", song)) == Qfalse)
97 continue;
98 TrackMediaItem * item = new TrackMediaItem(m_view);
99 item->setSong(new Value(song));
101 return true;
104 bool SonyNWMediaDevice::closeDevice()
106 QMutexLocker lock(&mutex);
107 delete m_db;
108 m_db = 0;
109 m_view->clear();
110 return true;
113 void SonyNWMediaDevice::synchronizeDevice()
115 QMutexLocker lock(&mutex);
116 m_db->send("save");
119 MediaItem *SonyNWMediaDevice::copyTrackToDevice( const MetaBundle& bundle )
121 QMutexLocker lock(&mutex);
122 // FIXME check if it's a file url
123 Value mp3 = klass("NetworkWalkman::AudioFile::Mp3").send("new", v(bundle.url().path()));
124 mp3.send("putOnDevice", v(mountPoint()));
125 m_db->send("add", mp3);
126 TrackMediaItem * item = new TrackMediaItem(m_view);
127 item->setSong(new Value(mp3));
128 return item;
131 int SonyNWMediaDevice::deleteItemFromDevice( MediaItem *it, int flags )
133 QMutexLocker lock(&mutex);
134 TrackMediaItem * item = dynamic_cast<TrackMediaItem *>(it);
135 if (item == 0)
136 return 0;
137 Value song = *(item->song());
138 m_db->send("remove", song);
139 /* debug() << "would remove: " << "\n";
140 debug() << "\t" << song.inspect() << "\n";
141 debug() << "item is: " << item << " == " << it << "\n";*/
142 delete it;
143 // debug() << "and deleted\n";
144 return 1;
147 #include "plugin_amarok_sonynw.moc"