summaryrefslogtreecommitdiff
path: root/src/tools/servomatic_gui.cc
blob: 610ba8005eb30aa5c70bac0e27e436571fa8069e (plain)
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
/*
    Copyright (C) 2012 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 <boost/thread.hpp>
#include <wx/taskbar.h>
#include <wx/icon.h>
#include "wx_util.h"
#include "lib/util.h"
#include "lib/server.h"
#include "lib/config.h"

using namespace std;
using namespace boost;

enum {
	ID_status = 1,
	ID_quit,
	ID_timer
};

class MemoryLog : public Log
{
public:

	string get () const {
		boost::mutex::scoped_lock (_mutex);
		return _log;
	}

private:
	void do_log (string m)
	{
		_log = m;
	}

	string _log;	
};

static MemoryLog memory_log;

class StatusDialog : public wxDialog
{
public:
	StatusDialog ()
		: wxDialog (0, wxID_ANY, _("DVD-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 ();

		Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update));
		_timer.Start (1000);
	}

private:
	void update (wxTimerEvent &)
	{
		_text->ChangeValue (std_to_wx (memory_log.get ()));
		_sizer->Layout ();
	}

	wxFlexGridSizer* _sizer;
	wxTextCtrl* _text;
	wxTimer _timer;
};

class TaskBarIcon : public wxTaskBarIcon
{
public:
	TaskBarIcon ()
	{
		wxIcon icon (std_to_wx ("taskbar_icon"));
		SetIcon (icon, std_to_wx ("DVD-o-matic encode server"));

		Connect (ID_status, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::status));
		Connect (ID_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (TaskBarIcon::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 (wxCommandEvent &)
	{
		StatusDialog* d = new StatusDialog;
		d->Show ();
	}

	void quit (wxCommandEvent &)
	{
		wxTheApp->ExitMainLoop ();
	}
};

class App : public wxApp
{
public:
	App ()
		: wxApp ()
		, _thread (0)
	{}

private:	
	
	bool OnInit ()
	{
		dvdomatic_setup ();

		new TaskBarIcon;

		_thread = new thread (bind (&App::main_thread, this));
		return true;
	}

	void main_thread ()
	{
		Server server (&memory_log);
		server.run (Config::instance()->num_local_encoding_threads ());
	}

	boost::thread* _thread;
};

IMPLEMENT_APP (App)