Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/home/vlg/develop/gwavmerger/src/gWavMerger.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // Generated by assa-genesis
00003 //------------------------------------------------------------------------------
00004 //                            gWavMerger.cpp
00005 //------------------------------------------------------------------------------
00006 // $Id: gWavMerger.cpp,v 1.6 2003/02/07 05:15:10 vlg Exp $
00007 //------------------------------------------------------------------------------
00008 //  Copyright (c) 2001,2002 by Vladislav Grinchenko
00009 //
00010 //  This program is free software; you can redistribute it and/or
00011 //  modify it under the terms of the GNU General Public License
00012 //  as published by the Free Software Foundation; either version
00013 //  2 of the License, or (at your option) any later version.
00014 //------------------------------------------------------------------------------
00015 // Date   : Wed Dec 19 03:45:18 2001
00016 //------------------------------------------------------------------------------
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #   include "config.h"
00020 #endif
00021 
00022 #include <iostream>
00023 #include <fstream>
00024 #include <unistd.h>             // access(2)
00025 #include <stdlib.h>             // system(3)
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  Static declarations mandated by Singleton class
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  Local Helpers
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                          Member Functions
00066 *******************************************************************************/
00067 
00068 
00072 
00073 gWavMerger::gWavMerger ()
00074     : m_exit_value (0), 
00075       m_log_level (-1),         // not set
00076       m_main_win (NULL),
00077       m_kit (NULL)
00078 {
00079     // ---Configuration---
00080     rm_opt ('f', "config-file"  );
00081     rm_opt ('n', "instance"     );
00082     rm_opt ('p', "port"         );
00083 
00084     // ---Process bookkeeping---
00085     rm_opt ('b', "daemon"       );
00086     rm_opt ('l', "pidfile"      );
00087     m_ommit_pidflock_flag = 0;  // skip PID file creation step
00088     
00089     /*
00090      * Disable all debugging. You can control these options from
00091      * ~/.gnome/gwavmerger configuration file.
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 //    delete m_kit;
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       TRACE    = 0x01
00230       APP      = 0x02
00231       USR1     = 0x04
00232       USR2     = 0x08
00233       USR3     = 0x10
00234       ALL_APPS = 0x1F
00235       ERROR    = 0x20
00236       FORK     = 0x40000
00237     */
00238 
00239     if (m_log_level >= 0) {
00240         switch (m_log_level) {
00241         case 5:
00242             m_debug_mask = ALL; // Log message flood!
00243         case 4:  
00244             m_debug_mask = 0x40023; // APP_APPS+FORK+ERRORS
00245             break;
00246         case 3:  
00247             m_debug_mask = 0x23; // APP+TRACE+ERRORS
00248             break;
00249         case 2:     
00250             m_debug_mask = 0x21; // TRACE+ERRORS
00251             break;
00252         case 1: 
00253             m_debug_mask = 0x22; // APP+ERRORS
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 gnomemm call 
00306 
00307 Main::Main(const Glib::ustring& app_id, 
00308            const Glib::ustring& app_version,
00309            const ModuleInfo& module_info,
00310            int argc, 
00311            char** argv,
00312            const struct poptOption* options,  // GNOME_PARAM_POPT_TABLE, ?
00313            int flags, 
00314            poptContext* return_ctx);
00315 
00316 translates into
00317 
00318 GnomeProgram* pProgram  = 
00319 gnome_program_init(app_id_, 
00320                    app_version_,
00321                    module_info.gobj(),
00322                    argc, 
00323                    argv,
00324                    GNOME_PARAM_POPT_TABLE, options,
00325                    GNOME_PARAM_POPT_FLAGS, flags,
00326                    0);
00327 
00328 libgnome manual describes it as:
00329 
00330 GnomeProgram* 
00331 gnome_program_init(const char *app_id,
00332                    const char *app_version,
00333                    const GnomeModuleInfo *module_info,
00334                    int argc,
00335                    char **argv,
00336                    const char *first_property_name,
00337                    ...);
00338 
00339 
00340 What I was suggested instead is to call is the following to make
00341 Yelp working:
00342 
00343 gnome_program_init (app_id, 
00344                     version, 
00345                     LIBGNOMEUI_MODULE, 
00346                     nargs, 
00347                     args,
00348                     GNOME_PROGRAM_STANDARD_PROPERTIES, 
00349                     NULL);
00350 
00351 or GNOME_PARAM_APP_DATADIR, DATADIR
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 ();  // Calling Gtk::Main::quit () terminates Gtk event loop
00418     REACTOR->stopReactor ();
00419 
00420     /* From Gtk man page:
00421      *  "Terminate the program and return the given exit code to the caller. 
00422      *   This function will shut down the GUI and free all resources allocated 
00423      *   for GTK."
00424      *
00425      * I don't think that's entirely true. MergeDialog is managed and
00426      * I don't see its detructor being called ever (not that I care).
00427      *
00428      */ 
00429     gtk_exit (0);
00430 }
00431 
00432 

Generated on Tue Feb 11 23:05:19 2003 for gwavmerger by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002