~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to data/script/fcitx-diagnose.sh

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
 
 
3
shopt -s extglob nullglob globstar
 
4
export TEXTDOMAIN=fcitx
 
5
 
 
6
__test_bash_unicode() {
 
7
    local magic_str='${1}'$'\xe4'$'\xb8'$'\x80'
 
8
    local magic_replace=${magic_str//\$\{/$'\n'$\{}
 
9
    ! [ "${magic_str}" = "${magic_replace}" ]
 
10
}
 
11
 
 
12
if type gettext &> /dev/null && __test_bash_unicode; then
 
13
    _() {
 
14
        gettext "$@"
 
15
    }
 
16
else
 
17
    _() {
 
18
        echo "$@"
 
19
    }
 
20
fi
 
21
 
 
22
#############################
 
23
# utility
 
24
#############################
 
25
 
 
26
add_and_check_file() {
 
27
    local prefix="$1"
 
28
    local file="$2"
 
29
    local inode
 
30
    inode="$(stat -L --printf='%i' "${file}" 2> /dev/null)" || return 0
 
31
    local varname="___add_and_check_file_${prefix}_${inode}"
 
32
    [ ! -z "${!varname}" ] && return 1
 
33
    eval "${varname}=1"
 
34
    return 0
 
35
}
 
36
 
 
37
print_array() {
 
38
    for ele in "$@"; do
 
39
        echo "${ele}"
 
40
    done
 
41
}
 
42
 
 
43
repeat_str() {
 
44
    local i
 
45
    local n="$1"
 
46
    local str="$2"
 
47
    local res=""
 
48
    for ((i = 0;i < n;i++)); do
 
49
        res="${res}${str}"
 
50
    done
 
51
    echo "${res}"
 
52
}
 
53
 
 
54
find_in_path() {
 
55
    local w="$1"
 
56
    local IFS=':'
 
57
    local p
 
58
    local f
 
59
    local fs
 
60
    for p in ${PATH}; do
 
61
        eval 'fs=("${p}/"'"${w}"')'
 
62
        for f in "${fs[@]}"; do
 
63
            echo "$f"
 
64
        done
 
65
    done
 
66
}
 
67
 
 
68
run_grep_fcitx() {
 
69
    "$@" | grep fcitx
 
70
}
 
71
 
 
72
get_config_dir() {
 
73
    local conf_option="$1"
 
74
    local default_name="$2"
 
75
    for path in "$(fcitx4-config "--${conf_option}" 2> /dev/null)" \
 
76
        "/usr/share/fcitx/${default_name}" \
 
77
        "/usr/local/share/fcitx/${default_name}"; do
 
78
        [ ! -z "${path}" ] && [ -d "${path}" ] && {
 
79
            echo "${path}"
 
80
            return 0
 
81
        }
 
82
    done
 
83
    return 1
 
84
}
 
85
 
 
86
get_from_config_file() {
 
87
    local file="$1"
 
88
    local key="$2"
 
89
    local value
 
90
    value=$(sed -ne "s=^${key}\=\(.*\)=\1=gp" "$file" 2> /dev/null)
 
91
    [ -z "$value" ] && return 1
 
92
    echo "${value}"
 
93
    return 0
 
94
}
 
95
 
 
96
 
 
97
#############################
 
98
# print
 
99
#############################
 
100
 
 
101
# tty and color
 
102
__istty=0
 
103
 
 
104
check_istty() {
 
105
    [ -t 1 ] && {
 
106
        __istty=1
 
107
    } || {
 
108
        __istty=0
 
109
    }
 
110
}
 
111
 
 
112
print_tty_ctrl() {
 
113
    ((__istty)) || return
 
114
    echo -ne '\e['"${1}"'m'
 
115
}
 
116
 
 
117
replace_reset() {
 
118
    local line
 
119
    local IFS=$'\n'
 
120
    if [ ! -z "$1" ]; then
 
121
        while read line; do
 
122
            echo "${line//$'\e'[0m/$'\e'[${1}m}"
 
123
        done
 
124
        [ -z "${line}" ] || {
 
125
            echo -n "${line//$'\e'[0m/$'\e'[${1}m}"
 
126
        }
 
127
    else
 
128
        cat
 
129
    fi
 
130
}
 
131
 
 
132
__replace_line() {
 
133
    local IFS=$'\n'
 
134
    local __line=${1//\$\{/$'\n'$\{}
 
135
    shift
 
136
    local __varname
 
137
    echo "${__line}" | while read __line; do
 
138
        if [[ ${__line} =~ ^\$\{([_a-zA-Z0-9]+)\} ]]; then
 
139
            __varname="${BASH_REMATCH[1]}"
 
140
            echo -n "${__line/\$\{${__varname}\}/${!__varname}}"
 
141
        else
 
142
            echo -n "${__line}"
 
143
        fi
 
144
    done
 
145
    echo
 
146
}
 
147
 
 
148
__replace_vars() {
 
149
    local IFS=$'\n'
 
150
    local __line
 
151
    while read __line; do
 
152
        __replace_line "${__line}" "$@"
 
153
    done
 
154
    [ -z "${__line}" ] || {
 
155
        echo -n "$(__replace_line "${__line}" "$@")"
 
156
    }
 
157
}
 
158
 
 
159
print_eval() {
 
160
    echo "$1" | __replace_vars "${@:2}"
 
161
}
 
162
 
 
163
# print inline
 
164
code_inline() {
 
165
    print_tty_ctrl '01;36'
 
166
    echo -n '`'"$1"'`' | replace_reset '01;36'
 
167
    print_tty_ctrl '0'
 
168
}
 
169
 
 
170
print_link() {
 
171
    local text="$1"
 
172
    local url="$2"
 
173
    print_tty_ctrl '01;33'
 
174
    echo -n "[$text]($url)" | replace_reset '01;33'
 
175
    print_tty_ctrl '0'
 
176
}
 
177
 
 
178
print_not_found() {
 
179
    print_eval "$(_ '${1} not found.')" "$(code_inline $1)"
 
180
}
 
181
 
 
182
# indent levels and list index counters
 
183
__current_level=0
 
184
__list_indexes=(0)
 
185
 
 
186
set_cur_level() {
 
187
    local level="$1"
 
188
    local indexes=()
 
189
    local i
 
190
    if ((level >= 0)); then
 
191
        ((__current_level = level))
 
192
        for ((i = 0;i <= __current_level;i++)); do
 
193
            ((indexes[i] = __list_indexes[i]))
 
194
        done
 
195
        __list_indexes=("${indexes[@]}")
 
196
    else
 
197
        ((__current_level = 0))
 
198
        __list_indexes=()
 
199
    fi
 
200
}
 
201
 
 
202
increase_cur_level() {
 
203
    local level="$1"
 
204
    ((level = __current_level + level))
 
205
    set_cur_level "$level"
 
206
}
 
207
 
 
208
# print blocks
 
209
__need_blank_line=0
 
210
 
 
211
write_paragraph() {
 
212
    local str="$1"
 
213
    local p1="$2"
 
214
    local p2="$3"
 
215
    local code="$4"
 
216
    local prefix="$(repeat_str "${__current_level}" "    ")"
 
217
    local line
 
218
    local i=0
 
219
    local whole_prefix
 
220
    local IFS=$'\n'
 
221
    ((__need_blank_line)) && echo
 
222
    [ -z "${code}" ] || print_tty_ctrl "${code}"
 
223
    {
 
224
        while read line; do
 
225
            ((i == 0)) && {
 
226
                whole_prefix="${prefix}${p1}"
 
227
            } || {
 
228
                whole_prefix="${prefix}${p2}"
 
229
            }
 
230
            ((i++))
 
231
            [ -z "${line}" ] && {
 
232
                echo
 
233
            } || {
 
234
                echo "${whole_prefix}${line}"
 
235
            }
 
236
        done | replace_reset "${code}"
 
237
    } <<EOF
 
238
${str}
 
239
EOF
 
240
    [ -z "${code}" ] || print_tty_ctrl "0"
 
241
    __need_blank_line=1
 
242
}
 
243
 
 
244
write_eval() {
 
245
    write_paragraph "$(print_eval "$@")"
 
246
}
 
247
 
 
248
write_error() {
 
249
    write_paragraph "**${1}**" "${2}" "${3}" '01;31'
 
250
}
 
251
 
 
252
write_error_eval() {
 
253
    write_error "$(print_eval "$@")"
 
254
}
 
255
 
 
256
write_quote_str() {
 
257
    local str="$1"
 
258
    increase_cur_level 1
 
259
    __need_blank_line=0
 
260
    echo
 
261
    write_paragraph "${str}" '' '' '01;35'
 
262
    echo
 
263
    __need_blank_line=0
 
264
    increase_cur_level -1
 
265
}
 
266
 
 
267
write_quote_cmd() {
 
268
    write_quote_str "$("$@" 2>&1)"
 
269
}
 
270
 
 
271
write_title() {
 
272
    local level="$1"
 
273
    local title="$2"
 
274
    local prefix='######'
 
275
    prefix="${prefix::$level}"
 
276
    ((__need_blank_line)) && echo
 
277
    print_tty_ctrl '01;34'
 
278
    echo "${prefix} ${title}" | replace_reset '01;34'
 
279
    print_tty_ctrl '0'
 
280
    __need_blank_line=0
 
281
    set_cur_level -1
 
282
}
 
283
 
 
284
write_order_list() {
 
285
    local str="$1"
 
286
    local index
 
287
    increase_cur_level -1
 
288
    increase_cur_level 1
 
289
    ((index = ++__list_indexes[__current_level - 1]))
 
290
    ((${#index} > 2)) && index="${index: -2}"
 
291
    index="${index}.   "
 
292
    increase_cur_level -1
 
293
    write_paragraph "${str}" "${index::4}" '    ' '01;32'
 
294
    increase_cur_level 1
 
295
}
 
296
 
 
297
write_order_list_eval() {
 
298
    write_order_list "$(print_eval "$@")"
 
299
}
 
300
 
 
301
# write_list() {
 
302
#     local str="$1"
 
303
#     increase_cur_level -1
 
304
#     write_paragraph "${str}" '*   ' '    ' '01;32'
 
305
#     increase_cur_level 1
 
306
# }
 
307
 
 
308
 
 
309
#############################
 
310
# print tips and links
 
311
#############################
 
312
 
 
313
wiki_url="http://fcitx-im.org/wiki"
 
314
 
 
315
beginner_guide_link() {
 
316
    print_link "$(_ "Beginner's Guide")" \
 
317
        "${wiki_url}$(_ /Beginner%27s_Guide)"
 
318
}
 
319
 
 
320
set_env_link() {
 
321
    local env_name="$1"
 
322
    local value="$2"
 
323
    local fmt
 
324
    fmt=$(_ 'Please set environment variable ${env_name} to "${value}" using the tool your distribution provides or add ${1} to your ${2}. See ${link}.')
 
325
    local link
 
326
    link=$(print_link \
 
327
        "$(_ "Input Method Related Environment Variables: ")${env_name}" \
 
328
        "${wiki_url}$(_ "/Input_method_related_environment_variables")#${env_name}")
 
329
    write_error_eval "${fmt}" "$(code_inline "export ${env_name}=${value}")" \
 
330
        "$(code_inline '~/.xprofile')"
 
331
}
 
332
 
 
333
gnome_36_link() {
 
334
    local link
 
335
    link=$(print_link \
 
336
        "$(_ "Note for GNOME Later than 3.6")" \
 
337
        "${wiki_url}$(_ "/Note_for_GNOME_Later_than_3.6")")
 
338
    local fmt
 
339
    fmt=$(_ 'If you are using ${1}, you may want to uninstall ${2} or remove ${3} to be able to use any input method other than ${2}. See ${link} for more detail as well as alternative solutions.')
 
340
    write_error_eval "${fmt}" "$(code_inline 'gnome>=3.6')" \
 
341
        "$(code_inline 'ibus')" "$(code_inline 'ibus-daemon')"
 
342
}
 
343
 
 
344
no_xim_link() {
 
345
    local fmt
 
346
    fmt=$(_ 'To see some application specific problems you may have when using xim, check ${link1}. For other more general problems of using XIM including application freezing, see ${link2}.')
 
347
    local link1
 
348
    link1=$(print_link \
 
349
        "$(_ "Hall of Shame for Linux IME Support")" \
 
350
        "${wiki_url}$(_ "/Hall_of_Shame_for_Linux_IME_Support")")
 
351
    local link2
 
352
    link2=$(print_link \
 
353
        "$(_ "here")" \
 
354
        "${wiki_url}$(_ "/XIM")")
 
355
    write_error_eval "${fmt}"
 
356
}
 
357
 
 
358
 
 
359
#############################
 
360
# system info
 
361
#############################
 
362
 
 
363
ldpaths=()
 
364
init_ld_paths() {
 
365
    local IFS=$'\n'
 
366
    local _ldpaths=($(ldconfig -p 2> /dev/null | grep '=>' | \
 
367
        sed -e 's:.* => \(.*\)/[^/]*$:\1:g' | sort -u)
 
368
        /lib /lib32 /lib64 /usr/lib /usr/lib32 /usr/lib64
 
369
        /usr/local/lib /usr/local/lib32 /usr/local/lib64)
 
370
    local path
 
371
    ldpaths=()
 
372
    for path in "${_ldpaths[@]}"; do
 
373
        [ -d "${path}" ] && add_and_check_file ldpath "${path}" && {
 
374
            ldpaths=("${ldpaths[@]}" "${path}")
 
375
        }
 
376
    done
 
377
}
 
378
init_ld_paths
 
379
 
 
380
check_system() {
 
381
    write_title 1 "$(_ "System Info.")"
 
382
    write_order_list "$(code_inline 'uname -a'):"
 
383
    if type uname &> /dev/null; then
 
384
        write_quote_cmd uname -a
 
385
    else
 
386
        write_error "$(print_not_found 'uname')"
 
387
    fi
 
388
    if type lsb_release &> /dev/null; then
 
389
        write_order_list "$(code_inline 'lsb_release -a'):"
 
390
        write_quote_cmd lsb_release -a
 
391
        write_order_list "$(code_inline 'lsb_release -d'):"
 
392
        write_quote_cmd lsb_release -d
 
393
    else
 
394
        write_order_list "$(code_inline lsb_release):"
 
395
        write_paragraph "$(print_not_found 'lsb_release')"
 
396
    fi
 
397
    write_order_list "$(code_inline /etc/lsb-release):"
 
398
    if [ -f /etc/lsb-release ]; then
 
399
        write_quote_cmd cat /etc/lsb-release
 
400
    else
 
401
        write_paragraph "$(print_not_found '/etc/lsb-release')"
 
402
    fi
 
403
    write_order_list "$(code_inline /etc/os-release):"
 
404
    if [ -f /etc/os-release ]; then
 
405
        write_quote_cmd cat /etc/os-release
 
406
    else
 
407
        write_paragraph "$(print_not_found '/etc/os-release')"
 
408
    fi
 
409
}
 
410
 
 
411
check_env() {
 
412
    write_title 1 "$(_ "Environment.")"
 
413
    write_order_list "DISPLAY:"
 
414
    write_quote_str "DISPLAY='${DISPLAY}'"
 
415
    write_order_list "$(_ "Keyboard Layout:")"
 
416
    increase_cur_level 1
 
417
    write_order_list "$(code_inline setxkbmap)"
 
418
    if type setxkbmap &> /dev/null; then
 
419
        write_quote_cmd setxkbmap -print
 
420
    else
 
421
        write_paragraph "$(print_not_found 'setxkbmap')"
 
422
    fi
 
423
    write_order_list "$(code_inline xprop)"
 
424
    if type xprop &> /dev/null; then
 
425
        write_quote_cmd xprop -root _XKB_RULES_NAMES
 
426
    else
 
427
        write_paragraph "$(print_not_found 'xprop')"
 
428
    fi
 
429
    increase_cur_level -1
 
430
    write_order_list "$(_ "Locale:")"
 
431
    if type locale &> /dev/null; then
 
432
        increase_cur_level 1
 
433
        write_order_list "$(_ "Current locale:")"
 
434
        write_quote_cmd locale
 
435
        write_order_list "$(_ "All locale:")"
 
436
        write_quote_cmd locale -a
 
437
        increase_cur_level -1
 
438
    else
 
439
        write_paragraph "$(print_not_found 'locale')"
 
440
    fi
 
441
}
 
442
 
 
443
check_fcitx() {
 
444
    local IFS=$'\n'
 
445
    write_title 1 "$(_ "Fcitx State.")"
 
446
    write_order_list "$(_ 'executable:')"
 
447
    if ! fcitx_exe="$(which fcitx 2> /dev/null)"; then
 
448
        write_error "$(_ "Cannot find fcitx executable!")"
 
449
        __need_blank_line=0
 
450
        write_error_eval "$(_ 'Please check ${1} for how to install fcitx.')" \
 
451
            "$(beginner_guide_link)"
 
452
        exit 1
 
453
    else
 
454
        write_eval "$(_ 'Found fcitx at ${1}.')" "$(code_inline "${fcitx_exe}")"
 
455
    fi
 
456
    write_order_list "$(_ 'version:')"
 
457
    version=$(fcitx -v 2> /dev/null | \
 
458
        sed -e 's/.*fcitx version: \([0-9.]*\).*/\1/g')
 
459
    write_eval "$(_ 'Fcitx version: ${version}')"
 
460
    write_order_list "$(_ 'process:')"
 
461
    psoutput=$(ps -Ao pid,comm)
 
462
    process=()
 
463
    while read line; do
 
464
        if [[ $line =~ ^([0-9]*)\ .*fcitx.* ]]; then
 
465
            [ "${BASH_REMATCH[1]}" = "$$" ] && continue
 
466
            process=("${process[@]}" "${line}")
 
467
        fi
 
468
    done <<EOF
 
469
${psoutput}
 
470
EOF
 
471
    if ! ((${#process[@]})); then
 
472
        write_error "$(_ "Fcitx is not running.")"
 
473
        __need_blank_line=0
 
474
        write_error_eval "$(_ 'Please check the Configure link of your distribution in ${1} for how to setup fcitx autostart.')" "$(beginner_guide_link)"
 
475
        return 1
 
476
    fi
 
477
    local pcount="${#process[@]}"
 
478
    if ((pcount > 1)); then
 
479
        write_eval "$(_ 'Found ${1} fcitx processes:')" "${#process[@]}"
 
480
    else
 
481
        write_eval "$(_ 'Found ${1} fcitx process:')" "${#process[@]}"
 
482
    fi
 
483
    write_quote_cmd print_array "${process[@]}"
 
484
    write_order_list "$(code_inline 'fcitx-remote'):"
 
485
    if type fcitx-remote &> /dev/null; then
 
486
        if ! fcitx-remote &> /dev/null; then
 
487
            write_error "$(_ "Cannot connect to fcitx correctly.")"
 
488
        else
 
489
            write_eval "$(_ '${1} works properly.')" \
 
490
                "$(code_inline 'fcitx-remote')"
 
491
        fi
 
492
    else
 
493
        write_error "$(print_not_found "fcitx-remote")"
 
494
    fi
 
495
}
 
496
 
 
497
 
 
498
#############################
 
499
# front end
 
500
#############################
 
501
 
 
502
_env_correct() {
 
503
    write_eval \
 
504
        "$(_ 'Environment variable ${1} is set to "${2}" correctly.')" \
 
505
        "$1" "$2"
 
506
}
 
507
 
 
508
_env_incorrect() {
 
509
    write_error_eval \
 
510
        "$(_ 'Environment variable ${1} is "${2}" instead of "${3}". Please check if you have exported it incorrectly in any of your init files.')" \
 
511
        "$1" "$3" "$2"
 
512
}
 
513
 
 
514
check_xim() {
 
515
    write_title 2 "Xim."
 
516
    xim_name=fcitx
 
517
    write_order_list "$(code_inline '${XMODIFIERS}'):"
 
518
    if [ -z "${XMODIFIERS}" ]; then
 
519
        set_env_link XMODIFIERS '@im=fcitx'
 
520
        __need_blank_line=0
 
521
    elif [ "${XMODIFIERS}" = '@im=fcitx' ]; then
 
522
        _env_correct 'XMODIFIERS' '@im=fcitx'
 
523
        __need_blank_line=0
 
524
    else
 
525
        _env_incorrect 'XMODIFIERS' '@im=fcitx' "${XMODIFIERS}"
 
526
        if [[ ${XMODIFIERS} =~ @im=([-_0-9a-zA-Z]+) ]]; then
 
527
            xim_name="${BASH_REMATCH[1]}"
 
528
        else
 
529
            __need_blank_line=0
 
530
            write_error_eval "$(_ 'Cannot interpret XMODIFIERS: ${1}.')" \
 
531
                "${XMODIFIERS}"
 
532
        fi
 
533
        if [ "${xim_name}" = "ibus" ]; then
 
534
            __need_blank_line=0
 
535
            gnome_36_link
 
536
        fi
 
537
    fi
 
538
    write_eval "$(_ 'Xim Server Name from Environment variable is ${1}.')" \
 
539
        "${xim_name}"
 
540
    write_order_list "$(_ 'XIM_SERVERS on root window:')"
 
541
    local atom_name=XIM_SERVERS
 
542
    if ! type xprop &> /dev/null; then
 
543
        write_error "$(print_not_found 'xprop')"
 
544
    else
 
545
        xprop=$(xprop -root -notype -f "${atom_name}" \
 
546
            '32a' ' $0\n' "${atom_name}" 2> /dev/null)
 
547
        if [[ ${xprop} =~ ^${atom_name}\ @server=(.*)$ ]]; then
 
548
            xim_server_name="${BASH_REMATCH[1]}"
 
549
            if [ "${xim_server_name}" = "${xim_name}" ]; then
 
550
                write_paragraph "$(_ "Xim server name is the same with that set in the environment variable.")"
 
551
            else
 
552
                write_error_eval "$(_ 'Xim server name: "${1}" is different from that set in the environment variable: "${2}".')" \
 
553
                    "${xim_server_name}" "${xim_name}"
 
554
            fi
 
555
        else
 
556
            write_error "$(_ "Cannot find xim_server on root window.")"
 
557
        fi
 
558
    fi
 
559
}
 
560
 
 
561
_check_toolkit_env() {
 
562
    local env_name="$1"
 
563
    local name="$2"
 
564
    write_order_list "$(code_inline '${'"${env_name}"'}'):"
 
565
    if [ -z "${!env_name}" ]; then
 
566
        set_env_link "${env_name}" 'fcitx'
 
567
    elif [ "${!env_name}" = 'fcitx' ]; then
 
568
        _env_correct "${env_name}" 'fcitx'
 
569
    else
 
570
        _env_incorrect "${env_name}" 'fcitx' "${!env_name}"
 
571
        __need_blank_line=0
 
572
        if [ "${!env_name}" = 'xim' ]; then
 
573
            write_error_eval "$(_ 'You are using xim in ${1} programs.')" \
 
574
                "${name}"
 
575
            no_xim_link
 
576
        else
 
577
            write_error_eval \
 
578
                "$(_ 'You may have trouble using fcitx in ${1} programs.')" \
 
579
                 "${name}"
 
580
            if [ "${!env_name}" = "ibus" ] && [ "${name}" = 'qt' ]; then
 
581
                __need_blank_line=0
 
582
                gnome_36_link
 
583
            fi
 
584
        fi
 
585
        set_env_link "${env_name}" 'fcitx'
 
586
    fi
 
587
}
 
588
 
 
589
check_qt() {
 
590
    write_title 2 "Qt."
 
591
    _check_toolkit_env QT_IM_MODULE qt
 
592
    qtimmodule_found=''
 
593
    write_order_list "$(_ 'Qt IM module files:')"
 
594
    echo
 
595
    for path in "${ldpaths[@]}"; do
 
596
        # the {/*,} here is for lib/$ARCH/ when output of ldconfig
 
597
        # failed to include it
 
598
        for file in "${path}"{/*,}/qt{,4}/**/*fcitx*.so; do
 
599
            if [[ ${file} =~ (im-fcitx|inputmethods) ]]; then
 
600
                __need_blank_line=0
 
601
                add_and_check_file qt "${file}" && {
 
602
                    write_eval "$(_ 'Found fcitx qt im module: ${1}.')" \
 
603
                        "$(code_inline "${file}")"
 
604
                }
 
605
                qtimmodule_found=1
 
606
            else
 
607
                __need_blank_line=0
 
608
                add_and_check_file qt "${file}" && {
 
609
                    write_eval \
 
610
                        "$(_ 'Found unknown fcitx qt module: ${1}.')" \
 
611
                        "$(code_inline "${file}")"
 
612
                }
 
613
            fi
 
614
        done
 
615
    done
 
616
    if [ -z "${qtimmodule_found}" ]; then
 
617
        __need_blank_line=0
 
618
        write_error "$(_ "Cannot find fcitx input method module for qt.")"
 
619
    fi
 
620
}
 
621
 
 
622
check_gtk_immodule() {
 
623
    local version="$1"
 
624
    local IFS=$'\n'
 
625
    local query_immodule
 
626
    local _query_immodule=($(find_in_path "gtk-query-immodules-${version}*"))
 
627
    local module_found=0
 
628
    local fcitx_gtk
 
629
    write_order_list "gtk ${version}:"
 
630
    [ ${#_query_immodule[@]} = 0 ] && {
 
631
        write_error_eval \
 
632
            "$(_ "Cannot find gtk-query-immodules for gtk ${1}")" \
 
633
            "${version}"
 
634
        return 1
 
635
    }
 
636
    local f
 
637
    for f in "${_query_immodule[@]}"; do
 
638
        add_and_check_file "gtk_immodule_${version}" "${f}" && {
 
639
            write_eval \
 
640
                "$(_ 'Found gtk-query-immodules for gtk ${1} at ${2}.')" \
 
641
                "${version}" "$(code_inline "${f}")"
 
642
            if fcitx_gtk=$("$f" | grep fcitx); then
 
643
                module_found=1
 
644
                __need_blank_line=0
 
645
                write_eval "$(_ 'Found fcitx im modules for gtk ${1}.')" \
 
646
                    "${version}"
 
647
                write_quote_str "${fcitx_gtk}"
 
648
            fi
 
649
        }
 
650
    done
 
651
    ((module_found)) || {
 
652
        write_error_eval \
 
653
            "$(_ 'Cannot find fcitx im module for gtk ${1}.')" \
 
654
            "${version}"
 
655
    }
 
656
}
 
657
 
 
658
check_gtk_immodule_cache() {
 
659
    local version="$1"
 
660
    local IFS=$'\n'
 
661
    local cache_found=0
 
662
    local fcitx_gtk
 
663
    write_order_list "gtk ${version}:"
 
664
    for path in /etc "${ldpaths[@]}"; do
 
665
        # the {/*,} here is for lib/$ARCH/ when output of ldconfig
 
666
        # failed to include it
 
667
        for file in "${path}"{/*,}/gtk{-${version}{.0,},}{/**,}/*immodules*; do
 
668
            [ -f "${file}" ] || continue
 
669
            add_and_check_file "gtk_immodule_cache_${version}" "${file}" || {
 
670
                continue
 
671
            }
 
672
            write_eval "$(_ 'Found immodules cache for gtk ${1} ${2}.')" \
 
673
                "${version}" "$(code_inline "${file}")"
 
674
            if fcitx_gtk=$(grep fcitx "${file}"); then
 
675
                cache_found=1
 
676
                __need_blank_line=0
 
677
                write_eval "$(_ 'Found fcitx in cache file ${1}:')" \
 
678
                    "$(code_inline "${file}")"
 
679
                write_quote_str "${fcitx_gtk}"
 
680
            fi
 
681
        done
 
682
    done
 
683
    ((cache_found)) || {
 
684
        write_error_eval \
 
685
            "$(_ 'Cannot find fcitx im module for gtk ${1} in cache.')" \
 
686
            "${version}"
 
687
    }
 
688
}
 
689
 
 
690
check_gtk() {
 
691
    write_title 2 "Gtk."
 
692
    _check_toolkit_env GTK_IM_MODULE gtk
 
693
    write_order_list "$(_ 'Gtk IM module files:')"
 
694
    increase_cur_level 1
 
695
    check_gtk_immodule 2
 
696
    check_gtk_immodule 3
 
697
    increase_cur_level -1
 
698
    write_order_list "$(_ 'Gtk IM module cache:')"
 
699
    increase_cur_level 1
 
700
    check_gtk_immodule_cache 2
 
701
    check_gtk_immodule_cache 3
 
702
    increase_cur_level -1
 
703
}
 
704
 
 
705
 
 
706
#############################
 
707
# fcitx modules
 
708
#############################
 
709
 
 
710
check_modules() {
 
711
    local addon_conf_dir
 
712
    write_title 2 "$(_ "Fcitx Addons:")"
 
713
    write_order_list "$(_ 'Addon Config Dir:')"
 
714
    addon_conf_dir="$(get_config_dir addonconfigdir addon)" || {
 
715
        write_error "$(_ "Cannot find fcitx addon config directory.")"
 
716
        return
 
717
    }
 
718
    local enabled_addon=()
 
719
    local disabled_addon=()
 
720
    local name
 
721
    local enable
 
722
    write_eval "$(_ 'Found fcitx addon config directory: ${1}.')" \
 
723
        "$(code_inline "${addon_conf_dir}")"
 
724
    write_order_list "$(_ 'Addon List:')"
 
725
    for file in "${addon_conf_dir}"/*.conf; do
 
726
        if ! name=$(get_from_config_file "${file}" Name); then
 
727
            write_error_eval \
 
728
                "$(_ 'Invalid addon config file ${1}.')" \
 
729
                "$(code_inline "${file}")"
 
730
            continue
 
731
        fi
 
732
        enable=$(get_from_config_file "${file}" Enabled)
 
733
        if [ -f ~/.config/fcitx/addon/${name}.conf ]; then
 
734
            _enable=$(get_from_config_file \
 
735
                ~/.config/fcitx/addon/${name}.conf Enabled)
 
736
            [ -z "${_enable}" ] || enable="${_enable}"
 
737
        fi
 
738
        if [ $(echo "${enable}" | sed -e 's/.*/\L&/g') = false ]; then
 
739
            disabled_addon=("${disabled_addon[@]}" "${name}")
 
740
        else
 
741
            enabled_addon=("${enabled_addon[@]}" "${name}")
 
742
        fi
 
743
    done
 
744
    increase_cur_level 1
 
745
    write_order_list_eval "$(_ 'Found ${1} enabled addons:')" \
 
746
        "${#enabled_addon[@]}"
 
747
    [ "${#enabled_addon[@]}" = 0 ] || {
 
748
        write_quote_cmd print_array "${enabled_addon[@]}"
 
749
    }
 
750
    write_order_list_eval "$(_ 'Found ${1} disabled addons:')" \
 
751
        "${#disabled_addon[@]}"
 
752
    [ "${#disabled_addon[@]}" = 0 ] || {
 
753
        write_quote_cmd print_array "${disabled_addon[@]}"
 
754
    }
 
755
    increase_cur_level -1
 
756
}
 
757
 
 
758
check_input_methods() {
 
759
    write_title 2 "$(_ "Input Methods:")"
 
760
    local IFS=','
 
761
    local imlist=($(get_from_config_file \
 
762
        ~/.config/fcitx/profile EnabledIMList)) || {
 
763
        write_error "$(_ "Cannot read im list from fcitx profile.")"
 
764
        return 0
 
765
    }
 
766
    local enabled_im=()
 
767
    local disabled_im=()
 
768
    local im
 
769
    local name
 
770
    local enable
 
771
    for im in "${imlist[@]}"; do
 
772
        [[ $im =~ ^([^:]+):(True|False)$ ]] || {
 
773
            write_error_eval "$(_ 'Invalid item ${1} in im list.')" \
 
774
                "${im}"
 
775
            continue
 
776
        }
 
777
        name="${BASH_REMATCH[1]}"
 
778
        if [ "${BASH_REMATCH[2]}" = True ]; then
 
779
            enabled_im=("${enabled_im[@]}" "${name}")
 
780
        else
 
781
            disabled_im=("${disabled_im[@]}" "${name}")
 
782
        fi
 
783
    done
 
784
    write_order_list_eval "$(_ 'Found ${1} enabled input methods:')" \
 
785
        "${#enabled_im[@]}"
 
786
    [ "${#enabled_im[@]}" = 0 ] || {
 
787
        write_quote_cmd print_array "${enabled_im[@]}"
 
788
    }
 
789
    write_order_list "$(_ 'Default input methods:')"
 
790
    case "${#enabled_im[@]}" in
 
791
        0)
 
792
            write_error "$(_ "You don't have any input methods enabled.")"
 
793
            ;;
 
794
        1)
 
795
            write_error "$(_ "You only have one input method enabled, please add a keyboard input method as the first one and your main input method as the second one.")"
 
796
            ;;
 
797
        *)
 
798
            if [[ ${enabled_im[0]} =~ ^fcitx-keyboard- ]]; then
 
799
                write_eval \
 
800
                    "$(_ 'You have a keyboard input method "${1}" correctly added as your default input method.')" \
 
801
                    "${enabled_im[0]}"
 
802
            else
 
803
                write_error_eval \
 
804
                    "$(_ 'Your first (default) input method is ${1} instead of a keyboard input method. You may have trouble deactivate fcitx.')" \
 
805
                    "${enabled_im[0]}"
 
806
            fi
 
807
            ;;
 
808
    esac
 
809
}
 
810
 
 
811
 
 
812
#############################
 
813
# log
 
814
#############################
 
815
 
 
816
check_log() {
 
817
    write_order_list "$(code_inline 'date')."
 
818
    if type date &> /dev/null; then
 
819
        write_quote_cmd date
 
820
    else
 
821
        write_error "$(print_not_found 'date')"
 
822
    fi
 
823
    write_order_list "$(code_inline '~/.config/fcitx/log/')."
 
824
    [ -d ~/.config/fcitx/log/ ] || {
 
825
        write_paragraph "$(print_not_found '~/.config/fcitx/log/')"
 
826
        return
 
827
    }
 
828
    write_quote_cmd ls -AlF ~/.config/fcitx/log/
 
829
    write_order_list "$(code_inline '~/.config/fcitx/log/crash.log')."
 
830
    if [ -f ~/.config/fcitx/log/crash.log ]; then
 
831
        write_quote_cmd cat ~/.config/fcitx/log/crash.log
 
832
    else
 
833
        write_paragraph "$(print_not_found '~/.config/fcitx/log/crash.log')"
 
834
    fi
 
835
}
 
836
 
 
837
 
 
838
#############################
 
839
# cmd line
 
840
#############################
 
841
 
 
842
_check_frontend=1
 
843
_check_modules=1
 
844
_check_log=1
 
845
[ -z "$1" ] || exec > "$1"
 
846
 
 
847
 
 
848
#############################
 
849
# init output
 
850
#############################
 
851
 
 
852
check_istty
 
853
 
 
854
 
 
855
#############################
 
856
# run
 
857
#############################
 
858
 
 
859
check_system
 
860
check_env
 
861
check_fcitx
 
862
 
 
863
((_check_frontend)) && {
 
864
    write_title 1 "$(_ "Frontends setup.")"
 
865
    check_xim
 
866
    check_qt
 
867
    check_gtk
 
868
}
 
869
 
 
870
((_check_modules)) && {
 
871
    write_title 1 "$(_ "Configuration.")"
 
872
    check_modules
 
873
    check_input_methods
 
874
}
 
875
 
 
876
((_check_log)) && {
 
877
    write_title 1 "$(_ "Log.")"
 
878
    check_log
 
879
}