1 #if defined(HAVE_CONFIG_H) && !defined(GEANYPY_WINDOWS)
2 # include "config.h"
3 #endif
4
5 #include "geanypy.h"
6
7
8 typedef struct
9 {
10 PyObject_HEAD
11 GeanyPrefs *prefs;
12 } Prefs;
13
14
15 static void
Prefs_dealloc(Prefs * self)16 Prefs_dealloc(Prefs *self)
17 {
18 g_return_if_fail(self != NULL);
19 self->ob_type->tp_free((PyObject *) self);
20 }
21
22
23 static int
Prefs_init(Prefs * self)24 Prefs_init(Prefs *self)
25 {
26 g_return_val_if_fail(self != NULL, -1);
27 self->prefs = geany_data->prefs;
28 return 0;
29 }
30
31
32 static PyObject *
Prefs_get_property(Prefs * self,const gchar * prop_name)33 Prefs_get_property(Prefs *self, const gchar *prop_name)
34 {
35 g_return_val_if_fail(self != NULL, NULL);
36 g_return_val_if_fail(prop_name != NULL, NULL);
37
38 if (!self->prefs)
39 {
40 PyErr_SetString(PyExc_RuntimeError,
41 "Prefs instance not initialized properly");
42 return NULL;
43 }
44
45 if (g_str_equal(prop_name, "default_open_path") && self->prefs->default_open_path)
46 return PyString_FromString(self->prefs->default_open_path);
47
48 Py_RETURN_NONE;
49 }
50 GEANYPY_PROPS_READONLY(Prefs);
51
52
53 static PyGetSetDef Prefs_getseters[] = {
54 GEANYPY_GETSETDEF(Prefs, "default_open_path",
55 "Default path to look for files when no other path is appropriate."),
56 { NULL }
57 };
58
59
60 static PyTypeObject PrefsType = {
61 PyObject_HEAD_INIT(NULL)
62 0, /* ob_size */
63 "geany.prefs.Prefs", /* tp_name */
64 sizeof(Prefs), /* tp_basicsize */
65 0, /* tp_itemsize */
66 (destructor) Prefs_dealloc, /* tp_dealloc */
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* tp_print - tp_as_buffer */
68 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
69 "Wrapper around a GeanyPrefs structure.", /* tp_doc */
70 0, 0, 0, 0, 0, 0, 0, 0, /* tp_traverse - tp_members */
71 Prefs_getseters, /* tp_getset */
72 0, 0, 0, 0, 0, /* tp_base - tp_dictoffset */
73 (initproc) Prefs_init, /* tp_init */
74 0, 0, /* tp_alloc - tp_new */
75 };
76
77
78 typedef struct
79 {
80 PyObject_HEAD
81 GeanyToolPrefs *tool_prefs;
82 } ToolPrefs;
83
84
85 static void
ToolPrefs_dealloc(ToolPrefs * self)86 ToolPrefs_dealloc(ToolPrefs *self)
87 {
88 g_return_if_fail(self != NULL);
89 self->ob_type->tp_free((PyObject *) self);
90 }
91
92
93 static int
ToolPrefs_init(ToolPrefs * self)94 ToolPrefs_init(ToolPrefs *self)
95 {
96 g_return_val_if_fail(self != NULL, -1);
97 self->tool_prefs = geany_data->tool_prefs;
98 return 0;
99 }
100
101
102 static PyObject *
ToolPrefs_get_property(ToolPrefs * self,const gchar * prop_name)103 ToolPrefs_get_property(ToolPrefs *self, const gchar *prop_name)
104 {
105 g_return_val_if_fail(self != NULL, NULL);
106 g_return_val_if_fail(prop_name != NULL, NULL);
107
108 if (!self->tool_prefs)
109 {
110 PyErr_SetString(PyExc_RuntimeError,
111 "ToolPrefs instance not initialized properly");
112 return NULL;
113 }
114
115 if (g_str_equal(prop_name, "browser_cmd") && self->tool_prefs->browser_cmd)
116 return PyString_FromString(self->tool_prefs->browser_cmd);
117 else if (g_str_equal(prop_name, "context_action_cmd") && self->tool_prefs->context_action_cmd)
118 return PyString_FromString(self->tool_prefs->context_action_cmd);
119 else if (g_str_equal(prop_name, "grep_cmd") && self->tool_prefs->grep_cmd)
120 return PyString_FromString(self->tool_prefs->grep_cmd);
121 else if (g_str_equal(prop_name, "term_cmd") && self->tool_prefs->term_cmd)
122 return PyString_FromString(self->tool_prefs->term_cmd);
123
124 Py_RETURN_NONE;
125 }
126 GEANYPY_PROPS_READONLY(ToolPrefs);
127
128
129 static PyGetSetDef ToolPrefs_getseters[] = {
130 GEANYPY_GETSETDEF(ToolPrefs, "browser_cmd", ""),
131 GEANYPY_GETSETDEF(ToolPrefs, "context_action_cmd", ""),
132 GEANYPY_GETSETDEF(ToolPrefs, "grep_cmd", ""),
133 GEANYPY_GETSETDEF(ToolPrefs, "term_cmd", ""),
134 { NULL }
135 };
136
137
138 static PyTypeObject ToolPrefsType = {
139 PyObject_HEAD_INIT(NULL)
140 0, /* ob_size */
141 "geany.prefs.ToolPrefs", /* tp_name */
142 sizeof(ToolPrefs), /* tp_basicsize */
143 0, /* tp_itemsize */
144 (destructor) ToolPrefs_dealloc, /* tp_dealloc */
145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* tp_print - tp_as_buffer */
146 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
147 "Wrapper around a GeanyToolPrefs structure.", /* tp_doc */
148 0, 0, 0, 0, 0, 0, 0, 0, /* tp_traverse - tp_members */
149 ToolPrefs_getseters, /* tp_getset */
150 0, 0, 0, 0, 0, /* tp_base - tp_dictoffset */
151 (initproc) ToolPrefs_init, /* tp_init */
152 0, 0, /* tp_alloc - tp_new */
153 };
154
155
156 #ifdef ENABLE_PRIVATE
157 static PyObject *
Prefs_show_dialog(PyObject * module)158 Prefs_show_dialog(PyObject *module)
159 {
160 prefs_show_dialog();
161 Py_RETURN_NONE;
162 }
163 #endif
164
165
166 static PyMethodDef PrefsModule_methods[] = {
167 #ifdef ENABLE_PRIVATE
168 { "show_dialog", (PyCFunction) Prefs_show_dialog, METH_NOARGS,
169 "Show the preferences dialog." },
170 #endif
171 { NULL }
172 };
173
174
initprefs(void)175 PyMODINIT_FUNC initprefs(void)
176 {
177 PyObject *m;
178
179 PrefsType.tp_new = PyType_GenericNew;
180 if (PyType_Ready(&PrefsType) < 0)
181 return;
182
183 ToolPrefsType.tp_new = PyType_GenericNew;
184 if (PyType_Ready(&ToolPrefsType) < 0)
185 return;
186
187 m = Py_InitModule3("prefs", PrefsModule_methods,
188 "General preferences dialog settings");
189
190 Py_INCREF(&PrefsType);
191 PyModule_AddObject(m, "Prefs", (PyObject *) &PrefsType);
192
193 Py_INCREF(&ToolPrefsType);
194 PyModule_AddObject(m, "ToolPrefs", (PyObject *) &ToolPrefsType);
195 }
196