1 /*
2  * imtim.c - Table-based Input Method for GTK+ 2 module.
3  *
4     Copyright (C) 2002-2003  Yao Zhang
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; version 2 of the License.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * yaoz@users.sourceforge.net
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <gtk/gtkimmodule.h>
26 
27 #include "gtkimcontexttim.h"
28 #include "timtable.h"
29 
30 #undef DEBUG
31 
32 static GPtrArray *INFO_ARRAY;
33 
im_module_init(GTypeModule * type_module)34 void im_module_init(GTypeModule *type_module)
35 {
36 #ifdef DEBUG
37 fprintf(stderr, "im_module_init()\n");
38 #endif
39     timtable_register_type(type_module);
40     gtkimcontexttim_register_type(type_module);
41 }
42 
im_module_exit(void)43 void im_module_exit(void)
44 {
45 #ifdef DEBUG
46 fprintf(stderr, "im_module_exit()\n");
47 #endif
48     if (INFO_ARRAY) {
49 	register int i;
50 	GtkIMContextInfo *info;
51 
52 	for (i = 0; i < INFO_ARRAY->len; i++) {
53 	    info = g_ptr_array_index(INFO_ARRAY, i);
54 	    if (info != NULL) {
55 		free((void *)info->context_id);
56 		free((void *)info->context_name);
57 		free((void *)info->domain);
58 		free((void *)info->domain_dirname);
59 		free((void *)info->default_locales);
60 	    }
61 	}
62         g_ptr_array_free(INFO_ARRAY, TRUE);
63 	INFO_ARRAY = NULL;
64     }
65 }
66 
im_module_list(const GtkIMContextInfo *** contexts,int * n_contexts)67 void im_module_list(const GtkIMContextInfo ***contexts,
68                     int *n_contexts)
69 {
70 #ifdef DEBUG
71 fprintf(stderr, "im_module_list()\n");
72 #endif
73     if (INFO_ARRAY == NULL) {
74 	int i;
75 	GtkIMContextInfo *info;
76 
77         INFO_ARRAY = g_ptr_array_new();
78 
79         info = g_new(GtkIMContextInfo, 1);
80 	info->context_id = strdup("table.tim");
81 	info->context_name = strdup("Table-based Input Method");
82 	info->domain = strdup("gtk+");
83 	info->domain_dirname = strdup("");
84 	info->default_locales = strdup("");
85 
86 	g_ptr_array_add(INFO_ARRAY, info);
87 
88 	for (i = 0; i < 2; i++) {
89 	    GString *path;
90 	    GDir *dir;
91 
92             path = g_string_new("");
93 	    switch (i) {
94 	    case 0:
95                 g_string_append(path, getenv("HOME"));
96                 g_string_append(path, "/.wenju");
97 		break;
98 	    case 1:
99                 g_string_append(path, PKGDATADIR);
100 		break;
101 	    default:
102 		break;
103 	    }
104 	    if ((dir = g_dir_open(path->str, 0, NULL)) != NULL) {
105 	        const gchar *entry;
106 
107 	        while ((entry = g_dir_read_name(dir)) != NULL) {
108 		    int len;
109 
110 		    len = strlen(entry);
111 		    if ((len > 4) && (strcmp(entry+len-4, ".tim") == 0)) {
112 		        GString *name;
113 
114 		        name = g_string_new("");
115 		        get_im_name(path->str, entry, name);
116 		        if (name->len > 0) {
117 			    g_string_append(name, " [TIM]");
118         		    info = g_new(GtkIMContextInfo, 1);
119 			    info->context_id = strdup(entry);
120 			    info->context_name = strdup(name->str);
121 			    info->domain = strdup("gtk+");
122 			    info->domain_dirname = strdup("");
123 			    info->default_locales = strdup("");
124 
125 			    g_ptr_array_add(INFO_ARRAY, info);
126 		        }
127                         g_string_free(name, TRUE);
128 		    }
129 	        }
130 	        g_dir_close(dir);
131 	    }
132             g_string_free(path, TRUE);
133 	}
134     }
135 
136     *contexts = (const GtkIMContextInfo **)INFO_ARRAY->pdata;
137     *n_contexts = INFO_ARRAY->len;
138 }
139 
im_module_create(const gchar * context_id)140 GtkIMContext *im_module_create(const gchar *context_id)
141 {
142     GtkIMContextTIM *im;
143 
144 #ifdef DEBUG
145 fprintf(stderr, "im_module_create()\n");
146 #endif
147     if ((context_id == NULL) || (context_id[0] == '\0')) {
148         return NULL;
149     }
150 
151     im = g_object_new(GTKIMCONTEXTTIM_TYPE, NULL);
152     GTKIMCONTEXTTIM_GET_CLASS(im)->set(im, context_id);
153 
154     return GTK_IM_CONTEXT(im);
155 }
156