Try to improve _server on OSX.
[dcpomatic.git] / src / tools / dcpomatic_server.cc
index d3a353154eba34e908eebda120f94373f836e012..978f600b2deecf829a256cc8451913ef07f16d1e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    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
 
 */
 
-#include <boost/thread.hpp>
-#include <wx/taskbar.h>
-#include <wx/icon.h>
-#include "wx_util.h"
+#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>
+#include <iostream>
 
 using std::cout;
 using std::string;
+using std::exception;
 using boost::shared_ptr;
 using boost::thread;
 using boost::bind;
@@ -46,22 +49,38 @@ public:
                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;    
+       string _log;
 };
 
 static shared_ptr<MemoryLog> memory_log (new MemoryLog);
 
+#ifdef DCPOMATIC_OSX
+class StatusDialog : public wxFrame
+#else
 class StatusDialog : public wxDialog
+#endif
 {
 public:
        StatusDialog ()
+#ifdef DCPOMATIC_OSX
+               : wxFrame (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
+#else
                : wxDialog (0, wxID_ANY, _("DCP-o-matic encode server"), wxDefaultPosition, wxSize (600, 80), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
+#endif
                , _timer (this, ID_timer)
        {
                _sizer = new wxFlexGridSizer (1, 6, 6);
@@ -73,12 +92,12 @@ public:
                SetSizer (_sizer);
                _sizer->Layout ();
 
-               Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update));
+               Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update, this), ID_timer);
                _timer.Start (1000);
        }
 
 private:
-       void update (wxTimerEvent &)
+       void update ()
        {
                _text->ChangeValue (std_to_wx (memory_log->get ()));
                _sizer->Layout ();
@@ -94,24 +113,33 @@ class TaskBarIcon : public wxTaskBarIcon
 public:
        TaskBarIcon ()
        {
-#ifdef __WXMSW__               
+#ifdef DCPOMATIC_WINDOWS
                wxIcon icon (std_to_wx ("taskbar_icon"));
 #endif
-#ifdef __WXGTK__
+#ifdef DCPOMATIC_LINUX
                wxInitAllImageHandlers();
-               wxBitmap bitmap (wxString::Format (wxT ("%s/taskbar_icon.png"), POSIX_ICON_PREFIX), wxBITMAP_TYPE_PNG);
+               wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), LINUX_SHARE_PREFIX), wxBITMAP_TYPE_PNG);
                wxIcon icon;
                icon.CopyFromBitmap (bitmap);
 #endif
-#ifndef __WXOSX__
-               /* XXX: fix this for OS X */
+#ifndef DCPOMATIC_OSX
                SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
-#endif         
+#endif
+
+#ifdef DCPOMATIC_OSX
+               wxMenu* file = new wxMenu;
+               file->Append (wxID_EXIT, _("&Exit"));
+               wxMenuBar* bar = new wxMenuBar;
+               bar->Append (file, _("&File"));
+               SetMenuBar (bar);
 
-               Connect (ID_status, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::status));
-               Connect (ID_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::quit));
+               status ();
+#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;
@@ -121,19 +149,19 @@ public:
        }
 
 private:
-       void status (wxCommandEvent &)
+       void status ()
        {
                StatusDialog* d = new StatusDialog;
                d->Show ();
        }
 
-       void quit (wxCommandEvent &)
+       void quit ()
        {
                wxTheApp->ExitMainLoop ();
        }
 };
 
-class App : public wxApp
+class App : public wxApp, public ExceptionStore
 {
 public:
        App ()
@@ -142,19 +170,26 @@ public:
                , _icon (0)
        {}
 
-private:       
-       
+private:
+
        bool OnInit ()
        {
                if (!wxApp::OnInit ()) {
                        return false;
                }
-               
+
+               dcpomatic_setup_path_encoding ();
+               dcpomatic_setup_i18n ();
                dcpomatic_setup ();
+               Config::drop ();
 
                _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;
        }
 
@@ -165,13 +200,29 @@ private:
        }
 
        void main_thread ()
-       {
-               Server server (memory_log);
+       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)