00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef HAVE_CONFIG_H
00019 # include "config.h"
00020 #endif
00021
00022 #include <iostream>
00023 #include <fstream>
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026
00027 #include <libgnomemm/program.h>
00028
00029 #include "gWavMerger-main.h"
00030 #include "gWavMerger.h"
00031
00032 #include <assa/CommonUtils.h>
00033 #include <assa/Logger.h>
00034 #include <assa/SigAction.h>
00035
00036 using ASSA::ALL;
00037
00038
00039
00040
00041
00042 gWavMerger* ASSA::Singleton<gWavMerger>::m_instance;
00043 ASSA::Destroyer<gWavMerger> ASSA::Singleton<gWavMerger>::m_destroyer;
00044 const int gWavMerger::TIMEOUT = 500;
00045
00046
00047
00048
00049 static int
00050 split_pair (const string& pair_, char sep_, string& lhs_, string& rhs_)
00051 {
00052 int pos = 0;
00053 if ((pos = pair_.find (sep_)) == (size_t)(-1)) {
00054 return -1;
00055 }
00056 lhs_ = pair_.substr (0, pos);
00057 rhs_ = pair_.substr (pos+1, pair_.size ());
00058 pos = rhs_.size () -1;
00059 if (rhs_[0] == '"' || rhs_[0] == '\'') rhs_[0] = ' ';
00060 if (rhs_[pos] == '"' || rhs_[pos] == '\'') rhs_[pos] = ' ';
00061 return 0;
00062 }
00063
00064
00065
00066
00067
00068
00072
00073 gWavMerger::gWavMerger ()
00074 : m_exit_value (0),
00075 m_log_level (-1),
00076 m_main_win (NULL),
00077 m_kit (NULL)
00078 {
00079
00080 rm_opt ('f', "config-file" );
00081 rm_opt ('n', "instance" );
00082 rm_opt ('p', "port" );
00083
00084
00085 rm_opt ('b', "daemon" );
00086 rm_opt ('l', "pidfile" );
00087 m_ommit_pidflock_flag = 0;
00088
00089
00090
00091
00092
00093 m_debug_mask = 0x0;
00094 m_log_file = "/dev/null";
00095
00096 add_opt (0, "gtk-options", &m_gtk_options);
00097 add_opt (0, "wavjoin-options", &m_wavjoin_options);
00098 add_opt (0, "log-level", &m_log_level);
00099 }
00100
00101
00102 gWavMerger::~gWavMerger ()
00103 {
00104 delete m_main_win;
00105
00106 }
00107
00112 bool
00113
00114 gWavMerger::timer_cb ()
00115 {
00116 ASSA::TimeVal timeout (0.01);
00117 REACTOR->waitForEvents (&timeout);
00118 return true;
00119 }
00120
00135 void
00136
00137 gWavMerger::init (int* argc_, char* argv_[], const char* help_info_)
00138 {
00139 trace ("gWavMerger::init");
00140
00141 register char token;
00142 const int size = 256;
00143 char line [size];
00144 bool parsing_options_section = false;
00145
00146 gchar* s = NULL;
00147 gboolean b = false;
00148 gint i = 0;
00149
00155 string localrc (ASSA::Utils::strenv ("$HOME/.gnome2/gwavmerger"));
00156 string defaultrc (GWMDATADIR);
00157 defaultrc += "/gwavmerger";
00158 std::ifstream infile;
00159
00160 if (::access (localrc.c_str (), R_OK) < 0) {
00161 DL((TRACE,"No ~/.gnome2/gwavmerger found.\n"));
00162 DL((TRACE,"Installing from default.\n"));
00163
00164 if (::access (defaultrc.c_str (), R_OK) == 0) {
00165 string cmd ("cp -p ");
00166 cmd += defaultrc + " " + localrc;
00167 if (::system (cmd.c_str ()) < 0) {
00168 goto done;
00169 }
00170 }
00171 else {
00172 DL((TRACE,"Default configuration file is missing!\n"));
00173 }
00174 }
00175 infile.open (localrc.c_str (), std::ios::in);
00176 if (!infile.is_open ()) {
00177 goto done;
00178 }
00179
00180 for (token = infile.peek (); token != EOF; token = infile.peek ()) {
00181 while (token == '#' || token == '\n') {
00182 infile.getline (line, size, '\n');
00183 token = infile.peek ();
00184 }
00185 if (token == '[') {
00186 infile.getline (line, size, '\n');
00187 parsing_options_section = (line == string ("[options]"));
00188 }
00189 else {
00190 if (parsing_options_section) {
00191 infile.getline (line, size, '\n');
00192 std::string name, value;
00193 split_pair (line, '=', name, value);
00194
00195 if (name == "gtk_options") {
00196 m_gtk_options = value;
00197 }
00198 else if ( name == "wavjoin_options") {
00199 m_wavjoin_options = value;
00200 }
00201 else if ( name == "log_file") {
00202 m_log_file = value;
00203 }
00204 else if ( name == "log_stdout") {
00205 m_log_stdout_flag = (value == "1");
00206 }
00207 else if ( name == "log_size") {
00208 m_log_size = strtol (value.c_str (), NULL, 16); ;
00209 }
00210
00211 else if ( name == "mask") {
00212 m_debug_mask = strtol (value.c_str (), NULL, 16);
00213 }
00214
00215 else if ( name == "log_level") {
00216 m_log_level = strtol (value.c_str (), NULL, 16);
00217 }
00218 }
00219 else {
00220 infile.getline (line, size, '\n');
00221 }
00222 }
00223 }
00224
00225 done:
00226 ASSA::GenServer::init (argc_, argv_, help_info_);
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 if (m_log_level >= 0) {
00240 switch (m_log_level) {
00241 case 5:
00242 m_debug_mask = ALL;
00243 case 4:
00244 m_debug_mask = 0x40023;
00245 break;
00246 case 3:
00247 m_debug_mask = 0x23;
00248 break;
00249 case 2:
00250 m_debug_mask = 0x21;
00251 break;
00252 case 1:
00253 m_debug_mask = 0x22;
00254 break;
00255 case 0:
00256 default:
00257 m_debug_mask = 0x0;
00258 }
00259
00260 }
00261
00262 LOGGER->enable_groups (m_debug_mask);
00263 infile.close ();
00264 }
00265
00277 void
00278
00279 gWavMerger::initServer ()
00280 {
00281 trace("gWavMerger::initServer");
00282 ASSA::Log::disable_timestamp ();
00283
00284 DL((ALL,"gtk_options = \"%s\"\n", m_gtk_options.c_str ()));
00285 DL((ALL,"wavjoin_options = \"%s\"\n", m_wavjoin_options.c_str ()));
00286 DL((ALL,"log_stdout = %s\n", m_log_stdout_flag ? "true" : "false"));
00287 DL((ALL,"log_file = \"%s\"\n", m_log_file.c_str ()));
00288 DL((ALL,"log_size = %d\n", m_log_size));
00289 DL((ALL,"log_level = \"%d\"\n", m_log_level));
00290 DL((ALL,"mask = 0x%X\n", m_debug_mask));
00291
00292 int gtk_argc = 0;
00293 char** gtk_argv = NULL;
00294
00295 ASSA::SigAction old_chld_act;
00296 struct sigaction* sa;
00297
00298 old_chld_act.retrieve_action (SIGCHLD);
00299 sa = old_chld_act;
00300 m_gtk_options = "gwavmerger " + m_gtk_options;
00301
00302 ASSA::CmdLineOpts::str_to_argv (m_gtk_options, gtk_argc, gtk_argv);
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 m_kit = new Gnome::Main (PACKAGE, VERSION,
00356 Gnome::UI::module_info_get (),
00357 gtk_argc, gtk_argv);
00358
00359 #if SUSPEND
00360 gnome_program_init (PACKAGE, VERSION,
00361 LIBGNOMEUI_MODULE,
00362 gtk_argc, gtk_argv,
00363 GNOME_PARAM_APP_DATADIR, DATADIR,
00364 NULL);
00365 #endif
00366 ASSA::CmdLineOpts::free_argv (gtk_argv);
00367
00368 DL((APP,"AppModuleName = \"%s\"\n",
00369 Gnome::UI::module_info_get ().get_name ().c_str ()));
00370
00374 ASSA::SigAction ignore (SIG_IGN);
00375 ignore.register_action (SIGCHLD, &old_chld_act);
00376
00377 m_main_win = new MainWindow;
00378
00379 SigC::Slot0<bool> tslot = slot (*this, &gWavMerger::timer_cb);
00380 SigC::Connection conn = Glib::signal_timeout().connect (tslot, TIMEOUT);
00381
00382 DL((APP,"\n= Environment variables set by Autoconfig: =\n"
00383 "\tGWMDATADIR = \"%s\"\n"
00384 "\tDATADIR = \"%s\"\n"
00385 "\tPACKAGE = \"%s\"\n"
00386 "\tVERSION = \"%s\"\n\n",
00387 GWMDATADIR, DATADIR, PACKAGE, VERSION));
00388
00389 #if 0
00390
00392 Glib::ustring ustr;
00393 Glib::ustring file ("gwavmerger.xml");
00394 Glib::RefPtr<Gnome::Program> prog = Gnome::Program::get ();
00395
00396 ustr = prog->locate_file (Gnome::FILE_DOMAIN_DATADIR, file);
00397 DL((APP,"FILE_DOMAIN_DATADIR:gwavmerger = \"%s\"\n", ustr.c_str ()));
00398
00399 ustr = prog->locate_file (Gnome::FILE_DOMAIN_APP_DATADIR, file);
00400 DL((APP,"FILE_DOMAIN_APP_DATADIR:gwavmerger = \"%s\"\n", ustr.c_str ()));
00401 #endif
00402
00403 DL((APP,"Service has been initialized\n"));
00404 }
00405
00411 void
00412
00413 gWavMerger::processServer ()
00414 {
00415 trace("gWavMerger::processServer");
00416
00417 m_kit->run ();
00418 REACTOR->stopReactor ();
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 gtk_exit (0);
00430 }
00431
00432