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
|
/*
Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "wx/wx_util.h"
#include "lib/util.h"
#include "lib/server.h"
#include "lib/config.h"
#include "lib/log.h"
#include <wx/taskbar.h>
#include <wx/icon.h>
#include <boost/thread.hpp>
using std::cout;
using std::string;
using std::exception;
using boost::shared_ptr;
using boost::thread;
using boost::bind;
enum {
ID_status = 1,
ID_quit,
ID_timer
};
class MemoryLog : public Log
{
public:
string get () const {
boost::mutex::scoped_lock (_mutex);
return _log;
}
string head_and_tail (int amount = 1024) const {
if (int (_log.size ()) < (2 * amount)) {
return _log;
}
return _log.substr (0, amount) + _log.substr (_log.size() - amount - 1, amount);
}
private:
void do_log (string m)
{
_log = m;
}
string _log;
};
static shared_ptr<MemoryLog> memory_log (new MemoryLog);
class StatusDialog : public wxDialog
{
public:
StatusDialog ()
: wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
, _timer (this, ID_timer)
{
_sizer = new wxFlexGridSizer (1, 6, 6);
_sizer->AddGrowableCol (0, 1);
_text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
_sizer->Add (_text, 1, wxEXPAND);
SetSizer (_sizer);
_sizer->Layout ();
Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update, this), ID_timer);
_timer.Start (1000);
}
private:
void update ()
{
_text->ChangeValue (std_to_wx (memory_log->get ()));
_sizer->Layout ();
}
wxFlexGridSizer* _sizer;
wxTextCtrl* _text;
wxTimer _timer;
};
class TaskBarIcon : public wxTaskBarIcon
{
public:
TaskBarIcon ()
{
#ifdef __WXMSW__
wxIcon icon (std_to_wx ("taskbar_icon"));
#endif
#ifdef __WXGTK__
wxInitAllImageHandlers();
wxBitmap bitmap (wxString::Format (wxT ("%s/taskbar_icon.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
wxIcon icon;
icon.CopyFromBitmap (bitmap);
#endif
#ifndef __WXOSX__
/* XXX: fix this for OS X */
SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
#endif
Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
}
wxMenu* CreatePopupMenu ()
{
wxMenu* menu = new wxMenu;
menu->Append (ID_status, std_to_wx ("Status..."));
menu->Append (ID_quit, std_to_wx ("Quit"));
return menu;
}
private:
void status ()
{
StatusDialog* d = new StatusDialog;
d->Show ();
}
void quit ()
{
wxTheApp->ExitMainLoop ();
}
};
class App : public wxApp, public ExceptionStore
{
public:
App ()
: wxApp ()
, _thread (0)
, _icon (0)
{}
private:
bool OnInit ()
{
if (!wxApp::OnInit ()) {
return false;
}
dcpomatic_setup_path_encoding ();
dcpomatic_setup ();
_icon = new TaskBarIcon;
_thread = new thread (bind (&App::main_thread, this));
Bind (wxEVT_TIMER, boost::bind (&App::check, this));
_timer.reset (new wxTimer (this));
_timer->Start (1000);
return true;
}
int OnExit ()
{
delete _icon;
return wxApp::OnExit ();
}
void main_thread ()
try {
Server server (memory_log, false);
server.run (Config::instance()->num_local_encoding_threads ());
} catch (...) {
store_current ();
}
void check ()
{
try {
rethrow ();
} catch (exception& e) {
error_dialog (0, std_to_wx (e.what ()));
wxTheApp->ExitMainLoop ();
} catch (...) {
error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
wxTheApp->ExitMainLoop ();
}
}
boost::thread* _thread;
TaskBarIcon* _icon;
shared_ptr<wxTimer> _timer;
};
IMPLEMENT_APP (App)
|