forked from sgomin/ddb_medialib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.cpp
305 lines (260 loc) · 7.29 KB
/
plugin.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "plugin.hpp"
#include "medialib.h"
#include "main_widget.hpp"
#include "database.hpp"
#include "scan_thread.hpp"
#include <sys/types.h>
#include "deadbeef/gtkui_api.h"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#include <iostream>
#include <memory.h>
const std::string CONFIG_FILENAME = "medialib";
const std::string DB_FILENAME = "medialib.db";
class Plugin::Impl
{
public:
Impl();
~Impl();
int connect();
int disconnect();
Settings getSettings() const;
void storeSettings(Settings settings);
private:
static ddb_gtkui_widget_t * createWidget();
static void destroyWidget(ddb_gtkui_widget_t *w);
typedef std::unique_ptr<ScanThread> ScanThreadPtr;
#ifdef USE_GTK2
std::unique_ptr<Gtk::Main> app_;
#else
Glib::RefPtr<Gtk::Application> app_;
#endif
static ScanEventQueue eventQueue_;
static ddb_gtkui_t * pGtkUi_;
const fs::path fnSettings_;
static SettingsProvider settings_;
static DbOwnerPtr db_;
static ScanThreadPtr pScanThread_;
static MainWidget * pMainWidget_;
};
ScanEventQueue Plugin::Impl::eventQueue_;
ddb_gtkui_t * Plugin::Impl::pGtkUi_ = nullptr;
SettingsProvider Plugin::Impl::settings_;
DbOwnerPtr Plugin::Impl::db_;
Plugin::Impl::ScanThreadPtr Plugin::Impl::pScanThread_;
MainWidget * Plugin::Impl::pMainWidget_ = nullptr;
std::unique_ptr<Plugin::Impl> Plugin::s_pImpl;
//static
int Plugin::start()
{
try
{
std::clog << "[" PLUGIN_NAME " ] Starting plugin" << std::endl;
s_pImpl.reset(new Impl());
return 0;
}
catch(const std::exception & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to start plugin: "
<< ex.what() << std::endl;
return -1;
}
}
//static
int Plugin::stop()
{
try
{
std::clog << "[" PLUGIN_NAME " ] Stopping plugin" << std::endl;
s_pImpl.reset();
return 0;
}
catch(const std::exception & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to stop plugin: "
<< ex.what() << std::endl;
return -1;
}
}
//static
int Plugin::connect()
{
std::clog << "[" PLUGIN_NAME " ] Connecting plugin" << std::endl;
return s_pImpl->connect();
}
//static
int Plugin::disconnect ()
{
std::clog << "[" PLUGIN_NAME " ] Disconnecting plugin" << std::endl;
return s_pImpl->disconnect();
}
// static
Settings Plugin::getSettings()
{
return s_pImpl->getSettings();
}
// static
void Plugin::storeSettings(Settings settings)
{
s_pImpl->storeSettings(std::move(settings));
}
Plugin::Impl::Impl()
: fnSettings_(fs::path(deadbeef->get_config_dir()) / CONFIG_FILENAME)
{
Settings settings;
settings.load(fnSettings_.string());
settings_.setSettings(std::move(settings));
}
Plugin::Impl::~Impl()
{
assert(!pScanThread_);
}
namespace {
Extensions getSupportedExtensions()
{
Extensions extensions;
struct DB_decoder_s **decoders = deadbeef->plug_get_decoder_list();
for (size_t i = 0; decoders[i]; i++)
{
const gchar **exts = decoders[i]->exts;
for (size_t j = 0; exts[j]; j++)
{
std::string ext = std::string(".") + exts[j];
extensions.insert(std::move(ext));
}
}
return extensions;
}
}
int Plugin::Impl::connect()
try
{
const fs::path pathDb = fs::path(deadbeef->get_config_dir()) / DB_FILENAME;
std::clog << "[" PLUGIN_NAME " ] Opening database at " << pathDb << std::endl;
db_.reset(new DbOwner(pathDb.string()));
pGtkUi_ = (ddb_gtkui_t *) deadbeef->plug_get_for_id(DDB_GTKUI_PLUGIN_ID);
if (pGtkUi_)
{
std::clog << "[" PLUGIN_NAME " ] Found '" DDB_GTKUI_PLUGIN_ID "' plugin "
<< pGtkUi_->gui.plugin.version_major << "."
<< pGtkUi_->gui.plugin.version_minor << std::endl;
if (pGtkUi_->gui.plugin.version_major < 2)
{
std::cerr << "[" PLUGIN_NAME " ] Error: incompatible version of '"
<< DDB_GTKUI_PLUGIN_ID << "' plugin!" <<std::endl;
return -1;
}
#ifdef USE_GTK2
int argc = 0;
char args[] = "";
char* argv = args;
char** pargv = &argv;
app_.reset( new Gtk::Main(argc, pargv) );
#else
app_ = Gtk::Application::create();
#endif
pGtkUi_->w_reg_widget(
PLUGIN_NAME, DDB_WF_SINGLE_INSTANCE, &createWidget, "medialib", NULL);
}
else
{
std::cerr << "[" PLUGIN_NAME " ] Error: could not find '"
DDB_GTKUI_PLUGIN_ID "' plugin (gtkui api version "
<< DDB_GTKUI_API_VERSION_MAJOR
<< "." << DDB_GTKUI_API_VERSION_MINOR << ")!" << std::endl;
return -1;
}
std::clog << "[" PLUGIN_NAME " ] Successfully connected" << std::endl;
return 0;
}
catch(const DbException & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to open database: "
<< ex.what() << std::endl;
return -1;
}
catch(const std::exception & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to connect plugin: "
<< ex.what() << std::endl;
return -1;
}
int Plugin::Impl::disconnect()
try
{
std::clog << "Stopping scanning thread" << std::endl;
pScanThread_.reset();
std::clog << "[" PLUGIN_NAME " ] Closing database " << std::endl;
db_.reset();
if (pMainWidget_)
{
pMainWidget_->onDisconnect();
}
return 0;
}
catch(const DbException & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to close database: "
<< ex.what() << std::endl;
return -1;
}
catch(const std::exception & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to stop scan thread: "
<< ex.what() << std::endl;
return -1;
}
// static
ddb_gtkui_widget_t * Plugin::Impl::createWidget()
try
{
std::clog << "[" PLUGIN_NAME " ] Creating widget " << std::endl;
ddb_gtkui_widget_t *w =
static_cast<ddb_gtkui_widget_t*>(malloc(sizeof(ddb_gtkui_widget_t)));
memset(w, 0, sizeof (*w));
pMainWidget_ = new MainWidget(
db_->createReader(), eventQueue_, deadbeef->get_config_dir());
std::clog << "[" PLUGIN_NAME " ] Starting scan thread " << std::endl;
pScanThread_.reset(new ScanThread(
settings_,
getSupportedExtensions(),
*db_,
eventQueue_,
pMainWidget_->getOnChangedDisp(),
pMainWidget_->getActiveRecords()));
w->widget = GTK_WIDGET( pMainWidget_->gobj() );
w->destroy = &destroyWidget;
pGtkUi_->w_override_signals (w->widget, w);
return w;
}
catch(const DbException & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Failed to open database: "
<< ex.what() << std::endl;
return nullptr;
}
catch(const std::exception & ex)
{
std::cerr << "[" PLUGIN_NAME " ] Initialisation error: "
<< ex.what() << std::endl;
return nullptr;
}
// static
void Plugin::Impl::destroyWidget(ddb_gtkui_widget_t * w)
{
std::clog << "[" PLUGIN_NAME " ] Stopping scan thread " << std::endl;
pScanThread_.reset();
std::clog << "[" PLUGIN_NAME " ] Destroying widget " << std::endl;
delete Glib::wrap(w->widget);
pMainWidget_ = nullptr;
}
Settings Plugin::Impl::getSettings() const
{
return settings_.getSettings();
}
void Plugin::Impl::storeSettings(Settings settings)
{
settings.save(fnSettings_.string());
settings_.setSettings(std::move(settings));
pScanThread_->restart();
}