예제 #1
0
retval_t ScimBridgeAgentImpl::initialize_scim ()
{
    scim_bridge_pdebugln (6, "Initializing scim...");

    // Get system language.
    scim_language = scim_get_locale_language (scim_get_current_locale ());

    // Get imengine modules
    vector<String> imengine_module_names;
    scim_get_imengine_module_list (imengine_module_names);
    if (find (imengine_module_names.begin (), imengine_module_names.end (), "socket") == imengine_module_names.end ()) {
        scim_bridge_perrorln ("There is no socket frontend of IMEngines for SCIM...");
        return RETVAL_FAILED;
    }

	imengine_module_names.clear();
	imengine_module_names.push_back("socket");

    // Get config modules
    vector<String> config_module_names;
    scim_get_config_module_list (config_module_names);

    if (find (config_module_names.begin (), config_module_names.end (), "socket") == config_module_names.end ()) {
        scim_bridge_perrorln ("There is no socket frontend of config module for SCIM...");
        return RETVAL_FAILED;
    }
    
    // If there is no socket frontend running, launch one.
    if (!is_socket_frontend_ready ()) {
        scim_bridge_pdebugln (8, "Launching a SCIM daemon with Socket FrontEnd...");
        const String server_config_module_name = scim_global_config_read (SCIM_GLOBAL_CONFIG_DEFAULT_CONFIG_MODULE, String ("simple"));
        char* new_argv [] = {const_cast<char*>("--no-stay"),const_cast<char*>("-d"), NULL};
        scim_launch (true, server_config_module_name.c_str (), "all", "socket", new_argv);

        // Wait until the connection is established.
        for (int i = 0; i < 100; ++i) {
            if (is_socket_frontend_ready ()) {
                break;
            } else if (i < 99) {
                usleep (100000);
            } else {
                scim_bridge_perrorln ("Cannot establish the socket connection...");
                return RETVAL_FAILED;
            }
        }
    }
    
    {
    	scim_bridge_pdebugln (8, "Launching a SCIM daemon with x11 FrontEnd...");
    	char* new_argv [] = {const_cast<char*>("--no-stay"),const_cast<char*>("-d"), NULL};
    	scim_launch (true, "socket", "socket", "x11", new_argv);
    }

    // load config module
    scim_bridge_pdebugln (2, "Loading Config module...: socket");
    scim_config_module = new ConfigModule ("socket");

    //create config instance
    if (scim_config_module != NULL && scim_config_module->valid ()) {
        scim_config = scim_config_module->create_config ();
    } else {
        scim_bridge_pdebugln (2, "Cannot load the socket config module...");
        return RETVAL_FAILED;
    }

    // create backend
    scim_backend = new CommonBackEnd (scim_config, imengine_module_names);

    scim_bridge_pdebugln (4, "Initialize scim, done!");
    return RETVAL_SUCCEEDED;
}
예제 #2
0
int main (int argc, char *argv [])
{
    static ConfigModule  config_module;

    ConfigPointer        config;
    std::vector<String>  config_list;
    String               def_config;
    String               key;
    String               value;
    String               display;

    DataType             type = DATA_TYPE_STRING;
    Command              cmd = DO_NOTHING;
    bool                 reload = false;
    bool                 global = false;

    int                  i;

    char *p =  getenv ("DISPLAY");
    if (p) display = String (p);

    //get modules list
    scim_get_config_module_list (config_list);

    //Use simple Config module as default if available.
    if (config_list.size ()) {
        def_config = scim_global_config_read (SCIM_GLOBAL_CONFIG_DEFAULT_CONFIG_MODULE, String ("simple"));
        if (std::find (config_list.begin (),
                       config_list.end (),
                       def_config) == config_list.end ())
            def_config = config_list [0];
    } else {
        cerr << "No config module found.\n";
        return -1;
    }

    // parse command options
    i = 0;
    while (i<argc) {
        if (++i >= argc) break;

        if (String ("-h") == argv [i] ||
                String ("--help") == argv [i]) {
            cout << "Usage: " << argv [0] << " <option>...\n\n"
                 << "The options are:\n"
                 << "  -g, --get key        Get the value of this key.\n"
                 << "  -s, --set key=value  Set the value of this key.\n"
                 << "  -d, --del key        Delete the key and its data\n"
                 << "                       from user config file.\n"
                 << "  -t, --type type      The key value type, valid types are:\n"
                 << "                       string, int, double, bool, string-list,\n"
                 << "                       int-list. The default type is string.\n"
                 << "  -c, --config name    Use specified Config module,\n"
                 << "                       use simple module by default.\n"
                 << "                       Use \"global\" instead of a real config module name,\n"
                 << "                       if you want to access the global configuration file.\n"
                 << "                       (Normally they are /etc/scim/global and ~/.scim/global).\n"
                 << "  --reload             Force the running scim to reload configuration.\n"
                 << "  --display display    The display which scim Panel is running on,\n"
                 << "                       it's only useful when --reload is used.\n"
                 << "  -h, --help           Show this help.\n";
            return 0;
        }

        if (String ("-g") == argv [i] ||
                String ("--get") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            if (cmd != DO_NOTHING) {
                cerr << "You can only do one thing at the same time!\n";
                return -1;
            }
            key = String (argv [i]);
            cmd = GET_DATA;
            continue;
        }

        if (String ("-s") == argv [i] ||
                String ("--set") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            if (cmd != DO_NOTHING) {
                cerr << "You can only do one thing at the same time!\n";
                return -1;
            }

            String str (argv [i]);
            str = trim_blank (str);

            key = get_param_portion (str);
            value = get_value_portion (str);

            if (!key.length ()) {
                cerr << "Bad argument for option " << argv [i-1] << endl;
                return -1;
            }

            cmd = SET_DATA;
            continue;
        }

        if (String ("-d") == argv [i] ||
                String ("--del") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            if (cmd != DO_NOTHING) {
                cerr << "You can only do one thing at the same time!\n";
                return -1;
            }

            key = String (argv [i]);
            cmd = DEL_KEY;
            continue;
        }

        if (String ("-t") == argv [i] ||
                String ("--type") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            if (String (argv [i]) == "string")
                type = DATA_TYPE_STRING;
            else if (String (argv [i]) == "int")
                type = DATA_TYPE_INT;
            else if (String (argv [i]) == "double")
                type = DATA_TYPE_DOUBLE;
            else if (String (argv [i]) == "bool")
                type = DATA_TYPE_BOOL;
            else if (String (argv [i]) == "string-list")
                type = DATA_TYPE_STRING_LIST;
            else if (String (argv [i]) == "int-list")
                type = DATA_TYPE_STRING_LIST;
            else {
                cerr << "Bad argument for option " << argv [i-1] << endl;
                return -1;
            }
            continue;
        }

        if (String ("-c") == argv [i] ||
                String ("--config") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            def_config = String (argv [i]);
            continue;
        }

        if (String ("--display") == argv [i]) {
            if (++i >= argc) {
                cerr << "No argument for option " << argv [i-1] << endl;
                return -1;
            }
            display = String (argv [i]);
            continue;
        }

        if (String ("--reload") == argv [i]) {
            reload = true;
            continue;
        }

        cerr << "Unknown option " << argv [i] << endl;
        return -1;
    }

    if ((cmd == DO_NOTHING || !key.length ()) && reload == false) {
        cerr << "What do you want to do?\n";
        return -1;
    }

    if (cmd != DO_NOTHING) {
        if (def_config == "global") {
            global = true;
        } else {
            if (!config_module.load (def_config)) {
                cerr << "Failed to load config module " << def_config << endl;
                return -1;
            }

            config = config_module.create_config ();

            if (config.null ()) {
                cerr << "Failed to create config object.\n";
                return -1;
            }
        }
    }

    // Get data
    if (cmd == GET_DATA) {
        bool ok = false;
        if (type == DATA_TYPE_STRING) {
            if (global) {
                value = scim_global_config_read (key, String (""));
                ok = (scim_global_config_read (key, String ("Invalid")) == value);
            } else {
                ok = config->read (key, &value);
            }
            if (ok) cout << value << endl;
        } else if (type == DATA_TYPE_INT) {
            int intval;
            if (global) {
                intval = scim_global_config_read (key, (int) 0);
                ok = (scim_global_config_read (key, (int) 0) == intval);
            } else {
                ok = config->read (key, &intval);
            }
            if (ok) cout << intval << endl;
        } else if (type == DATA_TYPE_DOUBLE) {
            double doubleval;
            if (global) {
                doubleval = scim_global_config_read (key, (double) 0);
                ok = (scim_global_config_read (key, (double) 1) == doubleval);
            } else {
                ok = config->read (key, &doubleval);
            }
            if (ok) cout << doubleval << endl;
        } else if (type == DATA_TYPE_BOOL) {
            bool boolval;
            if (global) {
                boolval = scim_global_config_read (key, (bool) false);
                ok = (scim_global_config_read (key, (bool) true) == boolval);
            } else {
                ok = config->read (key, &boolval);
            }
            if (ok) cout << (boolval ? "true" : "false") << endl;
        } else if (type == DATA_TYPE_STRING_LIST) {
            std::vector <String> strlistval;
            if (global) {
                strlistval = scim_global_config_read (key, strlistval);
                ok = (strlistval.size () > 0);
            } else {
                ok = config->read (key, &strlistval);
            }
            if (ok) cout << scim_combine_string_list (strlistval, ',') << endl;
        } else if (type == DATA_TYPE_INT_LIST) {
            std::vector <int> intlistval;
            if (global) {
                intlistval = scim_global_config_read (key, intlistval);
                ok = (intlistval.size () > 0);
            } else {
                ok = config->read (key, &intlistval);
            }

            if (ok) {
                for (size_t i = 0; i<intlistval.size (); ++i) {
                    cout << intlistval [i];
                    if (i < intlistval.size () - 1)
                        cout << ",";
                }
                cout << endl;
            }
        }

        if (!ok) {
            cerr << "Failed to get key value.\n";
            return -1;
        }
    }

    // Set data
    else if (cmd == SET_DATA) {
        bool ok = true;
        if (type != DATA_TYPE_STRING && !value.length ()) {
            ok = false;
        } else if (type == DATA_TYPE_STRING) {
            if (global) {
                scim_global_config_write (key, value);
            } else {
                ok = config->write (key, value);
            }
        } else if (type == DATA_TYPE_INT) {
            int intval = strtol (value.c_str (), 0, 10);
            if (global) {
                scim_global_config_write (key, intval);
            } else {
                ok = config->write (key, intval);
            }
        } else if (type == DATA_TYPE_DOUBLE) {
            double doubleval = strtod (value.c_str (), 0);
            if (global) {
                scim_global_config_write (key, doubleval);
            } else {
                ok = config->write (key, doubleval);
            }
        } else if (type == DATA_TYPE_BOOL) {
            bool boolval = false;
            if (value == "true" || value == "True" || value == "TRUE" || value == "1")
                boolval = true;

            if (global) {
                scim_global_config_write (key, boolval);
            } else {
                ok = config->write (key, boolval);
            }
        } else if (type == DATA_TYPE_STRING_LIST) {
            std::vector <String> strlistval;
            scim_split_string_list (strlistval, value, ',');

            if (global) {
                scim_global_config_write (key, strlistval);
            } else {
                ok = config->write (key, strlistval);
            }
        } else if (type == DATA_TYPE_INT_LIST) {
            std::vector <int> intlistval;
            std::vector <String> strlist;
            scim_split_string_list (strlist, value, ',');
            for (size_t i = 0; i<strlist.size (); ++i)
                intlistval.push_back (strtol (strlist[i].c_str (), 0, 10));

            if (global) {
                scim_global_config_write (key, intlistval);
            } else {
                ok = config->write (key, intlistval);
            }
        }

        if (global)
            ok = scim_global_config_flush ();

        if (!ok) {
            cerr << "Failed to set key value.\n";
            return -1;
        } else {
            cout << "Set data success.\n";
            if (!global) config->flush ();
        }
    }

    // Delete key
    else if (cmd == DEL_KEY) {
        bool ok = false;

        if (global) {
            scim_global_config_reset (key);
            ok = scim_global_config_flush ();
        } else {
            ok = config->erase (key);
        }

        if (ok) {
            cout << "Delete key success.\n";
            if (!global) config->flush ();
        } else {
            cerr << "Failed to delete the key.\n";
            return -1;
        }
    }

    if (reload) {
        HelperInfo   helper_info ("41b79480-c5d2-4929-9456-11d519c26b87", "scim-config-agent", "", "", SCIM_HELPER_STAND_ALONE);
        HelperAgent  helper_agent;

        int          id;

        id = helper_agent.open_connection (helper_info, display);

        if (id < 0) {
            cerr << "Unable to open the connection to scim Panel.\n";
            return -1;
        }

        helper_agent.reload_config ();

        cout << "Configuration reload request has been sent to the running scim.\n";

        helper_agent.close_connection ();
    }

    return 0;
}