2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "wx/wx_util.h"
21 #include "wx/wx_signal_manager.h"
23 #include "lib/server.h"
24 #include "lib/config.h"
26 #include "lib/signaller.h"
27 #include <wx/taskbar.h>
29 #include <boost/thread.hpp>
30 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
47 static int const log_lines = 32;
49 class MemoryLog : public Log, public Signaller
55 BOOST_FOREACH (string const & i, _log) {
61 string head_and_tail (int) const {
66 boost::signals2::signal<void(string)> Appended;
67 boost::signals2::signal<void(int)> Removed;
70 void do_log (string m)
73 emit (boost::bind (boost::ref (Appended), m));
74 if (_log.size() > log_lines) {
75 emit (boost::bind (boost::ref (Removed), _log.front().length()));
83 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
85 class StatusDialog : public wxDialog
90 0, wxID_ANY, _("DCP-o-matic encode server"),
91 wxDefaultPosition, wxDefaultSize,
92 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
95 _sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
96 _sizer->AddGrowableCol (0, 1);
99 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
100 int const height = (size.GetHeight() + 2) * log_lines;
101 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
103 _text = new wxTextCtrl (
104 this, wxID_ANY, std_to_wx (memory_log->get()), wxDefaultPosition, wxSize (-1, height),
105 wxTE_READONLY | wxTE_MULTILINE
108 _sizer->Add (_text, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
113 memory_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
114 memory_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
118 void appended (string s)
120 (*_text) << s << "\n";
125 _text->Remove (0, n + 1);
128 wxFlexGridSizer* _sizer;
132 class TaskBarIcon : public wxTaskBarIcon
138 #ifdef DCPOMATIC_WINDOWS
139 wxIcon icon (std_to_wx ("taskbar_icon"));
141 wxInitAllImageHandlers();
142 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
144 icon.CopyFromBitmap (bitmap);
147 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
149 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
150 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
153 wxMenu* CreatePopupMenu ()
155 wxMenu* menu = new wxMenu;
156 menu->Append (ID_status, std_to_wx ("Status..."));
157 menu->Append (ID_quit, std_to_wx ("Quit"));
165 _status = new StatusDialog ();
172 wxTheApp->ExitMainLoop ();
175 StatusDialog* _status;
178 class App : public wxApp, public ExceptionStore
191 if (!wxApp::OnInit ()) {
195 dcpomatic_setup_path_encoding ();
196 dcpomatic_setup_i18n ();
200 signal_manager = new wxSignalManager (this);
201 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
203 _icon = new TaskBarIcon;
204 _thread = new thread (bind (&App::main_thread, this));
206 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
207 _timer.reset (new wxTimer (this));
208 _timer->Start (1000);
216 return wxApp::OnExit ();
221 Server server (memory_log, false);
222 server.run (Config::instance()->num_local_encoding_threads ());
231 } catch (exception& e) {
232 error_dialog (0, std_to_wx (e.what ()));
233 wxTheApp->ExitMainLoop ();
235 error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
236 wxTheApp->ExitMainLoop ();
242 signal_manager->ui_idle ();
245 boost::thread* _thread;
247 shared_ptr<wxTimer> _timer;