Improve exception handling in dcpomatic_server.
[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 private:
51         void do_log (string m)
52         {
53                 _log = m;
54         }
55
56         string _log;    
57 };
58
59 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
60
61 class StatusDialog : public wxDialog
62 {
63 public:
64         StatusDialog ()
65                 : wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
66                 , _timer (this, ID_timer)
67         {
68                 _sizer = new wxFlexGridSizer (1, 6, 6);
69                 _sizer->AddGrowableCol (0, 1);
70
71                 _text = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
72                 _sizer->Add (_text, 1, wxEXPAND);
73
74                 SetSizer (_sizer);
75                 _sizer->Layout ();
76
77                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update, this), ID_timer);
78                 _timer.Start (1000);
79         }
80
81 private:
82         void update ()
83         {
84                 _text->ChangeValue (std_to_wx (memory_log->get ()));
85                 _sizer->Layout ();
86         }
87
88         wxFlexGridSizer* _sizer;
89         wxTextCtrl* _text;
90         wxTimer _timer;
91 };
92
93 class TaskBarIcon : public wxTaskBarIcon
94 {
95 public:
96         TaskBarIcon ()
97         {
98 #ifdef __WXMSW__                
99                 wxIcon icon (std_to_wx ("taskbar_icon"));
100 #endif
101 #ifdef __WXGTK__
102                 wxInitAllImageHandlers();
103                 wxBitmap bitmap (wxString::Format (wxT ("%s/taskbar_icon.png"), POSIX_ICON_PREFIX), wxBITMAP_TYPE_PNG);
104                 wxIcon icon;
105                 icon.CopyFromBitmap (bitmap);
106 #endif
107 #ifndef __WXOSX__
108                 /* XXX: fix this for OS X */
109                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
110 #endif          
111
112                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
113                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
114         }
115         
116         wxMenu* CreatePopupMenu ()
117         {
118                 wxMenu* menu = new wxMenu;
119                 menu->Append (ID_status, std_to_wx ("Status..."));
120                 menu->Append (ID_quit, std_to_wx ("Quit"));
121                 return menu;
122         }
123
124 private:
125         void status ()
126         {
127                 StatusDialog* d = new StatusDialog;
128                 d->Show ();
129         }
130
131         void quit ()
132         {
133                 wxTheApp->ExitMainLoop ();
134         }
135 };
136
137 class App : public wxApp, public ExceptionStore
138 {
139 public:
140         App ()
141                 : wxApp ()
142                 , _thread (0)
143                 , _icon (0)
144         {}
145
146 private:        
147         
148         bool OnInit ()
149         {
150                 if (!wxApp::OnInit ()) {
151                         return false;
152                 }
153                 
154                 dcpomatic_setup ();
155
156                 _icon = new TaskBarIcon;
157                 _thread = new thread (bind (&App::main_thread, this));
158
159                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
160                 _timer.reset (new wxTimer (this));
161                 _timer->Start (1000);
162                 
163                 return true;
164         }
165
166         int OnExit ()
167         {
168                 delete _icon;
169                 return wxApp::OnExit ();
170         }
171
172         void main_thread ()
173         try {
174                 Server server (memory_log, false);
175                 server.run (Config::instance()->num_local_encoding_threads ());
176         } catch (...) {
177                 store_current ();
178         }
179
180         void check ()
181         {
182                 try {
183                         rethrow ();
184                 } catch (exception& e) {
185                         error_dialog (0, std_to_wx (e.what ()));
186                         wxTheApp->ExitMainLoop ();
187                 } catch (...) {
188                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
189                         wxTheApp->ExitMainLoop ();
190                 }
191         }
192
193         boost::thread* _thread;
194         TaskBarIcon* _icon;
195         shared_ptr<wxTimer> _timer;
196 };
197
198 IMPLEMENT_APP (App)