clean db
[platform/upstream/ibus-libpinyin.git] / src / PYUtil.h
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-pinyin - The Chinese PinYin engine for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #ifndef __PY_UTIL_H_
22 #define __PY_UTIL_H_
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #if defined(HAVE_UUID_CREATE)
29 #  include <uuid.h>
30 #elif defined(HAVE_LIBUUID)
31 #  include <uuid/uuid.h>
32 #endif
33
34 #include <sys/utsname.h>
35 #include <cstdlib>
36 #include <string>
37
38 #ifdef __GXX_EXPERIMENTAL_CXX0X__
39 #  include <memory>
40 #else
41 #  include <boost/shared_ptr.hpp>
42 #  include <boost/scoped_ptr.hpp>
43
44 namespace std {
45     // import boost::shared_ptr to std namespace
46     using boost::shared_ptr;
47     // import boost::scoped_ptr to std namespace, and rename to unique_ptr
48     // XXX: the unique_ptr can transfer the pointer ownership,
49     //      but scoped_ptr cannot.
50     template<typename T> class unique_ptr : public boost::scoped_ptr<T> {};
51 };
52
53 #endif
54
55 #include <ibus.h>
56
57 namespace PY {
58
59 // mask for Ctrl, Alt, Super, Hyper, Meta
60 const guint CMSHM_MASK = IBUS_CONTROL_MASK |
61                          IBUS_MOD1_MASK |
62                          IBUS_SUPER_MASK |
63                          IBUS_HYPER_MASK |
64                          IBUS_META_MASK;
65 // mask for Shift, Ctrl, Alt, Super, Hyper, Meta
66 const guint SCMSHM_MASK = CMSHM_MASK | IBUS_SHIFT_MASK;
67
68 inline guint
69 cmshm_filter (guint modifiers)
70 {
71     return modifiers & CMSHM_MASK;
72 }
73
74 inline guint
75 scmshm_filter (guint modifiers)
76 {
77     return modifiers & SCMSHM_MASK;
78 }
79
80 inline gboolean
81 cmshm_test (guint modifiers, guint mask)
82 {
83     return cmshm_filter (modifiers) == mask;
84 }
85
86 inline gboolean
87 scmshm_test (guint modifiers, guint mask)
88 {
89     return scmshm_filter (modifiers) == mask;
90 }
91
92 class UUID {
93 public:
94     UUID (void)
95     {
96         uuid_t u;
97 #if defined(HAVE_UUID_CREATE)
98         gchar* uuid;
99         uuid_create (&u, 0);
100         uuid_to_string (&u, &uuid, 0);
101         g_strlcpy (m_uuid, uuid, sizeof(m_uuid));
102         free(uuid);
103 #elif defined(HAVE_LIBUUID)
104         uuid_generate (u);
105         uuid_unparse_lower (u, m_uuid);
106 #endif
107     }
108
109     operator const gchar * (void) const
110     {
111         return m_uuid;        
112     }
113
114 private:
115     gchar m_uuid[256];
116 };
117
118 class Uname {
119 public:
120     Uname (void)
121     {
122         uname (&m_buf);
123     }
124
125     const gchar *hostname (void) const { return m_buf.nodename; }
126 private:
127     struct utsname m_buf;
128 };
129
130 class Hostname : public Uname {
131 public:
132     operator const gchar * (void) const
133     {
134         return hostname ();
135     }
136 };
137
138 class Env : public std::string {
139 public:
140     Env (const gchar *name)
141     {
142         gchar *str;
143         str = std::getenv (name);
144         assign (str != NULL ? str : "");
145     }
146
147     operator const gchar *(void) const
148     {
149         return c_str();
150     }
151 };
152
153 };
154 #endif