Fix log head/tail functions.
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <boost/thread.hpp>
21 #include <wx/taskbar.h>
22 #include <wx/icon.h>
23 #include "wx/wx_util.h"
24 #include "lib/util.h"
25 #include "lib/server.h"
26 #include "lib/config.h"
27
28 using std::cout;
29 using std::string;
30 using std::exception;
31 using boost::shared_ptr;
32 using boost::thread;
33 using boost::bind;
34
35 enum {
36         ID_status = 1,
37         ID_quit,
38         ID_timer
39 };
40
41 class MemoryLog : public Log
42 {
43 public:
44
45         string get () const {
46                 boost::mutex::scoped_lock (_mutex);
47                 return _log;
48         }
49
50         string head_and_tail (int amount = 1024) const {
51                 if (_log.size () < (2 * amount)) {
52                         return _log;
53                 }
54
55                 return _log.substr (0, amount) + _log.substr (_log.size() - amount - 1, amount);
56         }
57
58 private:
59         void do_log (string m)
60         {
61                 _log = m;
62         }
63
64         string _log;    
65 };
66
67 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
68
69 class StatusDialog : public wxDialog
70 {
71 public:
72         StatusDialog ()
73                 : wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
74                 , _timer (this, ID_timer)
75         {
76                 _sizer = new wxFlexGridSizer (1, 6, 6);
77                 _sizer->AddGrowableCol (0, 1);
78
79                 _text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
80                 _sizer->Add (_text, 1, wxEXPAND);
81
82                 SetSizer (_sizer);
83                 _sizer->Layout ();
84
85                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update, this), ID_timer);
86                 _timer.Start (1000);
87         }
88
89 private:
90         void update ()
91         {
92                 _text->ChangeValue (std_to_wx (memory_log->get ()));
93                 _sizer->Layout ();
94         }
95
96         wxFlexGridSizer* _sizer;
97         wxTextCtrl* _text;
98         wxTimer _timer;
99 };
100
101 class TaskBarIcon : public wxTaskBarIcon
102 {
103 public:
104         TaskBarIcon ()
105         {
106 #ifdef __WXMSW__                
107                 wxIcon icon (std_to_wx ("taskbar_icon"));
108 #endif
109 #ifdef __WXGTK__
110                 wxInitAllImageHandlers();
111                 wxBitmap bitmap (wxString::Format (wxT ("%s/taskbar_icon.png"), POSIX_ICON_PREFIX), wxBITMAP_TYPE_PNG);
112                 wxIcon icon;
113                 icon.CopyFromBitmap (bitmap);
114 #endif
115 #ifndef __WXOSX__
116                 /* XXX: fix this for OS X */
117                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
118 #endif          
119
120                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
121                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
122         }
123         
124         wxMenu* CreatePopupMenu ()
125         {
126                 wxMenu* menu = new wxMenu;
127                 menu->Append (ID_status, std_to_wx ("Status..."));
128                 menu->Append (ID_quit, std_to_wx ("Quit"));
129                 return menu;
130         }
131
132 private:
133         void status ()
134         {
135                 StatusDialog* d = new StatusDialog;
136                 d->Show ();
137         }
138
139         void quit ()
140         {
141                 wxTheApp->ExitMainLoop ();
142         }
143 };
144
145 class App : public wxApp, public ExceptionStore
146 {
147 public:
148         App ()
149                 : wxApp ()
150                 , _thread (0)
151                 , _icon (0)
152         {}
153
154 private:        
155         
156         bool OnInit ()
157         {
158                 if (!wxApp::OnInit ()) {
159                         return false;
160                 }
161                 
162                 dcpomatic_setup ();
163
164                 _icon = new TaskBarIcon;
165                 _thread = new thread (bind (&App::main_thread, this));
166
167                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
168                 _timer.reset (new wxTimer (this));
169                 _timer->Start (1000);
170                 
171                 return true;
172         }
173
174         int OnExit ()
175         {
176                 delete _icon;
177                 return wxApp::OnExit ();
178         }
179
180         void main_thread ()
181         try {
182                 Server server (memory_log, false);
183                 server.run (Config::instance()->num_local_encoding_threads ());
184         } catch (...) {
185                 store_current ();
186         }
187
188         void check ()
189         {
190                 try {
191                         rethrow ();
192                 } catch (exception& e) {
193                         error_dialog (0, std_to_wx (e.what ()));
194                         wxTheApp->ExitMainLoop ();
195                 } catch (...) {
196                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
197                         wxTheApp->ExitMainLoop ();
198                 }
199         }
200
201         boost::thread* _thread;
202         TaskBarIcon* _icon;
203         shared_ptr<wxTimer> _timer;
204 };
205
206 IMPLEMENT_APP (App)