summaryrefslogtreecommitdiff
path: root/ism/src/isf_pkg.cpp
blob: 9f1a059dacf0088250b434d0aa7e3f3ad86c27e3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */

/*
 * Smart Common Input Method
 *
 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id: scim_launcher.cpp,v 1.9 2005/06/15 00:19:08 suzhe Exp $
 *
 */

#define Uses_SCIM_HELPER

#include <unistd.h>
#include "scim_private.h"
#include "scim.h"
#if HAVE_PKGMGR_INFO
#include <pkgmgr-info.h>
#endif
#include <scim_panel_common.h>
#include <dlog.h>
#include <tzplatform_config.h>
#include <sys/stat.h>
#include "isf_query_utility.h"
#include "isf_pkg.h"
#include "isf_debug.h"

#ifdef LOG_TAG
# undef LOG_TAG
#endif
#define LOG_TAG             "ISF_PKG"

using namespace scim;

#if HAVE_PKGMGR_INFO
/**
 * @brief Read data from ime category manifest and insert initial db
 *
 * @param handle    pkgmgrinfo_appinfo_h pointer
 * @param user_data The data to pass to this callback.
 *
 * @return 0 if success, negative value(<0) if fail. Callback is not called if return value is negative.
 */
int isf_pkg_ime_app_list_cb (const pkgmgrinfo_appinfo_h handle, void *user_data)
{
    int ret = 0;
    char *appid = NULL, *pkgid = NULL, *pkgtype = NULL, *exec = NULL, *label = NULL, *path = NULL;
    pkgmgrinfo_pkginfo_h  pkginfo_handle = NULL;
    ImeInfoDB ime_db;
    int *result = static_cast<int*>(user_data); // 0: not IME, 1: Inserted, 2: Updated

    if (result) /* in this case, need to check category */ {
        bool exist = true;
        ret = pkgmgrinfo_appinfo_is_category_exist (handle, "http://tizen.org/category/ime", &exist);
        if (ret != PMINFO_R_OK || !exist) {
            return 0;
        }
    }

    /* appid */
    ret = pkgmgrinfo_appinfo_get_appid (handle, &appid);
    if (ret == PMINFO_R_OK)
        ime_db.appid = String (appid ? appid : "");
    else {
        LOGE ("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
        return 0;
    }

    ime_db.iconpath = "";

    /* pkgid */
    ret = pkgmgrinfo_appinfo_get_pkgid (handle, &pkgid);
    if (ret == PMINFO_R_OK)
        ime_db.pkgid = String (pkgid ? pkgid : "");
    else {
        LOGE ("pkgmgrinfo_appinfo_get_pkgid failed! error code=%d", ret);
        return 0;
    }

    /* exec path */
    ret = pkgmgrinfo_appinfo_get_exec (handle, &exec);
    if (ret == PMINFO_R_OK)
        ime_db.exec = String (exec ? exec : "");
    else {
        LOGE ("pkgmgrinfo_appinfo_get_exec failed! error code=%d", ret);
        return 0;
    }

    /* label */
    ret = pkgmgrinfo_appinfo_get_label (handle, &label);
    if (ret == PMINFO_R_OK)
        ime_db.label = String (label ? label : "");

    /* get pkgmgrinfo_pkginfo_h */
    /* Try to get in global packages */
    ret = pkgmgrinfo_pkginfo_get_pkginfo (pkgid, &pkginfo_handle);
    if (ret != PMINFO_R_OK) {
        /* Try to get in user packages */
        ret = pkgmgrinfo_pkginfo_get_usr_pkginfo (pkgid, getpid (), &pkginfo_handle);
    }

    if (ret == PMINFO_R_OK && pkginfo_handle) {
        /* pkgtype */
        ret = pkgmgrinfo_pkginfo_get_type (pkginfo_handle, &pkgtype);

        if (ret == PMINFO_R_OK)
            ime_db.pkgtype = String (pkgtype ? pkgtype : "");
        else {
            ISF_SAVE_LOG ("pkgtype is not available!");
            pkgmgrinfo_pkginfo_destroy_pkginfo (pkginfo_handle);
            return 0;
        }

        /* pkgrootpath */
        ret = pkgmgrinfo_pkginfo_get_root_path (pkginfo_handle, &path);
    }

    ime_db.languages = "en";
    ime_db.display_lang = "";

    if (ime_db.pkgtype.compare ("rpm") == 0 &&   //1 Inhouse IMEngine ISE(IME)
        ime_db.exec.find ("scim-launcher") != String::npos)  // Some IMEngine's pkgid doesn't have "ise-engine" prefix.
    {
        ime_db.mode = TOOLBAR_KEYBOARD_MODE;
        ime_db.options = 0;
        ime_db.module_path = String (SCIM_MODULE_PATH) + String (SCIM_PATH_DELIM_STRING) + String (SCIM_BINARY_VERSION)
            + String (SCIM_PATH_DELIM_STRING) + String ("IMEngine");
        ime_db.module_name = ime_db.pkgid;
        ime_db.is_enabled = 1;
        ime_db.is_preinstalled = 1;
        ime_db.has_option = 0; // It doesn't matter. No option for IMEngine...
    }
    else {
        ime_db.mode = TOOLBAR_HELPER_MODE;
        if (ime_db.pkgtype.compare ("rpm") == 0) //1 Inhouse Helper ISE(IME)
        {
#ifdef _TV
            ime_db.options = SCIM_HELPER_STAND_ALONE | SCIM_HELPER_NEED_SCREEN_INFO | SCIM_HELPER_AUTO_RESTART | ISM_HELPER_PROCESS_KEYBOARD_KEYEVENT;
#else
            ime_db.options = SCIM_HELPER_STAND_ALONE | SCIM_HELPER_NEED_SCREEN_INFO | SCIM_HELPER_AUTO_RESTART;
#endif
            ime_db.module_name = ime_db.pkgid;

            String module_path = String (path) + String ("/lib");
            String fullpath = module_path + String (SCIM_PATH_DELIM_STRING) + ime_db.module_name + String (".so");
            struct stat st;
            if (stat (fullpath.c_str (), &st) < 0) {
                /* Not found in lib directory of package's root path */
                ime_db.module_path = String (SCIM_MODULE_PATH) + String (SCIM_PATH_DELIM_STRING) + String (SCIM_BINARY_VERSION)
                    + String (SCIM_PATH_DELIM_STRING) + String ("Helper");
            }
            else {
                ime_db.module_path = module_path;
            }

            ime_db.is_enabled = 1;
            ime_db.is_preinstalled = 1;
            ime_db.has_option = 1;  // Let's assume the inhouse IME always has an option menu.
        }
        else if (ime_db.pkgtype.compare ("wgt") == 0)    //1 Download Web IME
        {
            ime_db.options = SCIM_HELPER_STAND_ALONE | SCIM_HELPER_NEED_SCREEN_INFO | SCIM_HELPER_AUTO_RESTART
                | SCIM_HELPER_NEED_SPOT_LOCATION_INFO | ISM_HELPER_PROCESS_KEYBOARD_KEYEVENT | ISM_HELPER_WITHOUT_IMENGINE;
            ime_db.module_path = String (SCIM_MODULE_PATH) + String (SCIM_PATH_DELIM_STRING) + String (SCIM_BINARY_VERSION)
                + String (SCIM_PATH_DELIM_STRING) + String ("Helper");
            ime_db.module_name = String ("ise-web-helper-agent");
            if (ime_db.exec.compare (0, 5, "/usr/") == 0) {
                ime_db.is_enabled = 1;
                ime_db.is_preinstalled = 1;
            }
            else {
#ifdef _MOBILE
               ime_db.is_enabled = 0;
#else
               ime_db.is_enabled = 1;
#endif
               ime_db.is_preinstalled = 0;
            }
            ime_db.has_option = -1; // At this point, we can't know IME has an option (setting) or not; -1 means unknown.
        }
        else if (ime_db.pkgtype.compare ("tpk") == 0)    //1 Download Native IME
        {
            ime_db.options = SCIM_HELPER_STAND_ALONE | SCIM_HELPER_NEED_SCREEN_INFO | SCIM_HELPER_AUTO_RESTART
                | ISM_HELPER_PROCESS_KEYBOARD_KEYEVENT | ISM_HELPER_WITHOUT_IMENGINE;
            if (path)
                ime_db.module_path = String (path) + String ("/lib");
            else
                ime_db.module_path = String (tzplatform_getenv(TZ_SYS_RW_APP)) + ime_db.pkgid + String ("/lib");
            ime_db.module_name = String ("lib") + ime_db.exec.substr (ime_db.exec.find_last_of (SCIM_PATH_DELIM) + 1);
            if (ime_db.exec.compare (0, 5, "/usr/") == 0) {
                ime_db.is_enabled = 1;
                ime_db.is_preinstalled = 1;
            }
            else {
#ifdef _MOBILE
                ime_db.is_enabled = 0;
#else
                ime_db.is_enabled = 1;
#endif
                ime_db.is_preinstalled = 0;
            }
            ime_db.has_option = -1; // At this point, we can't know IME has an option (setting) or not; -1 means unknown.

            String bin_path = String (path) + String (SCIM_PATH_DELIM_STRING) + String ("bin") + String (SCIM_PATH_DELIM_STRING) +
                              ime_db.exec.substr (ime_db.exec.find_last_of (SCIM_PATH_DELIM) + 1);

            struct stat st;
            if (stat (bin_path.c_str (), &st) < 0) {
                /* In case of no executable in bin directory of package's root path */
                /* Create symbolic link for launching and supporting application FW APIs */
                if (symlink (SCIM_HELPER_LAUNCHER_PROGRAM, bin_path.c_str ()) != 0)
                    LOGW ("Failed to create symbolic link : %s\n", bin_path.c_str ());
                else
                    LOGD ("Succeeded to create symbolic link : %s\n", bin_path.c_str ());
            }
        }
        else {
            LOGE ("Unsupported pkgtype(%s)", ime_db.pkgtype.c_str ());
            if (pkginfo_handle) {
                pkgmgrinfo_pkginfo_destroy_pkginfo (pkginfo_handle);
                pkginfo_handle = NULL;
            }
            return 0;
        }
    }

    ret = isf_db_insert_ime_info (&ime_db);
    if (ret < 1) {
        if (result) {
            ret = isf_db_update_ime_info (&ime_db);
            if (ret) {
                *result = 2;    // Updated
            }
        }
        if (ret < 1) {
            LOGE ("isf_db_%s_ime_info failed(%d). appid=%s pkgid=%s", (result? "update" : "insert"), ret, ime_db.appid.c_str(), ime_db.pkgid.c_str());
        }
    }
    else if (result && ret) {
        *result = 1;    // Inserted
    }

    if (pkginfo_handle) {
        pkgmgrinfo_pkginfo_destroy_pkginfo (pkginfo_handle);
        pkginfo_handle = NULL;
    }

    return 0;
}
#endif

/**
 * @brief Reload ime_info DB. This needs to be called when ime_info table is empty.
 */
void isf_pkg_reload_ime_info_db(void)
{
#if HAVE_PKGMGR_INFO
    pkgmgrinfo_appinfo_filter_h handle;
    int ret = pkgmgrinfo_appinfo_filter_create (&handle);
    if (ret == PMINFO_R_OK) {
        ret = pkgmgrinfo_appinfo_filter_add_string (handle, PMINFO_APPINFO_PROP_APP_CATEGORY, "http://tizen.org/category/ime");
        if (ret == PMINFO_R_OK) {
            ret = pkgmgrinfo_appinfo_filter_foreach_appinfo (handle, isf_pkg_ime_app_list_cb, NULL);
        }
        else {
            LOGE ("pkgmgrinfo_appinfo_filter_add_string failed(%d)", ret);
        }
        pkgmgrinfo_appinfo_filter_destroy (handle);
    }
    else {
        LOGE ("pkgmgrinfo_appinfo_filter_create failed(%d)", ret);
    }
#endif
}

/**
 * @brief Select all ime_info table. If ime_info table is empty, this will reload data through pkgmgr-info.
 *
 * @param ime_info The list to store ImeInfoDB
 *
 * @return the number of selected row.
 */
int isf_pkg_select_all_ime_info_db(std::vector<ImeInfoDB> &ime_info)
{
    int result = isf_db_select_all_ime_info(ime_info);
    if (result == 0) {   // Probably ime_info DB's already added by scim process. But isf_db_delete_ime_info() can delete it, so try to make one here.
        isf_pkg_reload_ime_info_db();

        return isf_db_select_all_ime_info(ime_info);
    }
    return result;
}

/*
vi:ts=4:ai:nowrap:expandtab
*/