File: Charset_List.py

package info (click to toggle)
xpn 1.2.6-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,500 kB
  • ctags: 1,685
  • sloc: python: 12,693; xml: 206; makefile: 16; sh: 1
file content (310 lines) | stat: -rw-r--r-- 13,708 bytes parent folder | download | duplicates (2)
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
import gtk
import gobject
import cPickle
import os
from locale import getdefaultlocale
from xpn_src.UserDir import get_wdir

try: system_enc=getdefaultlocale()[1]
except: system_enc="US-ASCII"
guess_list=[system_enc,"US-ASCII","ISO-8859-1","ISO-8859-2","ISO-8859-3","ISO-8859-4","ISO-8859-5",
                "ISO-8859-6","ISO-8859-7","ISO-8859-8","ISO-8859-9","ISO-8859-10","ISO-8859-13",
                "ISO-8859-14","ISO-8859-15","ISO-2022-JP","ISO-2022-KR","KOI8-R","KOI8-U","GB2312","BIG5",
                "WINDOWS-1250","WINDOWS-1251","WINDOWS-1252","WINDOWS-1253","WINDOWS-1254",
                "WINDOWS-1255","WINDOWS-1256","WINDOWS-1257","WINDOWS-1258","MACCYRILLIC","MACICELAND",
                "MACLATIN2","MACROMAN","MACTURKISH","UTF-8"]
encodings_list=["US-ASCII","ISO-8859-1","ISO-8859-2","ISO-8859-3","ISO-8859-4","ISO-8859-5",
                "ISO-8859-6","ISO-8859-7","ISO-8859-8","ISO-8859-9","ISO-8859-10","ISO-8859-13",
                "ISO-8859-14","ISO-8859-15","ISO-2022-JP","ISO-2022-KR","KOI8-R","KOI8-U","GB2312","BIG5",
                "UTF-7","UTF-8","WINDOWS-1250","WINDOWS-1251","WINDOWS-1252","WINDOWS-1253","WINDOWS-1254",
                "WINDOWS-1255","WINDOWS-1256","WINDOWS-1257","WINDOWS-1258","MACCYRILLIC","MACICELAND",
                "MACLATIN2","MACROMAN","MACTURKISH"]
encodings_tip="US-ASCII (English) \
        \nISO-8859-1 (West Europe, latin1) \
        \nISO-8859-2 (Central and Eastern Europe, latin2) \
        \nISO-8859-3 (Esperanto, Maltese, latin3) \
        \nISO-8859-4 (Baltic languages, latin4) \
        \nISO-8859-5 (Cyrillic) \
        \nISO-8859-6 (Arabic) \
        \nISO-8859-7 (Greek) \
        \nISO-8859-8 (Hebrew) \
        \nISO-8859-9 (Turkish, latin5) \
        \nISO-8859-10 (Nordish languages, latin6) \
        \nISO-8859-13 (Baltic languages) \
        \nISO-8859-14 (Celtic languages, latin8) \
        \nISO-8859-15 (Western Europe) \
        \nISO-2022-JP (Japanese) \
        \nISO-2022-KR (Korean) \
        \nKOI8-R (Russian) \
        \nKOI8-U (Ukrainian) \
        \nGB-2312 (Simplified Chinese) \
        \nBIG5 (Traditional Chinese) \
        \nUTF-7 (All languages) \
        \nUTF-8 (All languages) \
        \nWINDOWS-1250 (Central and Eastern Europe, cp1250) \
        \nWINDOWS-1251 (Cyrillic, cp1251) \
        \nWINDOWS-1252 (Western Europe, cp1252) \
        \nWINDOWS-1253 (Greek, cp1253) \
        \nWINDOWS-1254 (Turkish, cp1254) \
        \nWINDOWS-1255 (Hebrew, cp1255) \
        \nWINDOWS-1256 (Arabic, cp1256) \
        \nWINDOWS-1257 (Baltic Languages, cp1257) \
        \nWINDOWS-1258 (Vietnamese, cp1258) \
        \nMACCYRILLIC (Cyrillic) \
        \nMACICELAND (Icelandic) \
        \nMACLATIN2 (Central and Eastern Europe) \
        \nMACROMAN (Western Europe) \
        \nMACTURKISH (Turkish)"
ordered_list=["US-ASCII","ISO-8859-1","ISO-8859-2","ISO-8859-3","ISO-8859-4","ISO-8859-5",
                "ISO-8859-6","ISO-8859-7","ISO-8859-8","ISO-8859-9","ISO-8859-10","ISO-8859-13",
                "ISO-8859-14","ISO-8859-15","UTF-8"]

def load_ordered_list():
    try:
        f=open(os.path.join(get_wdir(),"dats/charset_list.dat"),"rb")
    except IOError:
        list=ordered_list
    else:
        list=cPickle.load(f)
        f.close()
        if not list:
            list=ordered_list
    return list

class CharsetList:

    def delete_event(self,widget,event,data=None):
        return False  

    def destroy(self,widget):
        self.window.destroy()
        if __name__=="__main__":
            gtk.main_quit()
    
    def close_ok(self,obj): 
        self.dump_tree(self.right_list)
        self.destroy(None)
    
    def close_cancel(self,obj):
        self.destroy(None)
         
    def fill_lists(self):
        for encoding in encodings_list:
            self.left_model.append([encoding])

        list=load_ordered_list()
        for encoding in list:
            self.right_model.append([encoding])

    def get_charset_list(self,treeview):
        model=treeview.get_model()
        iter=model.get_iter_first()
        list=[]
        while iter:
            list.append(model.get_value(iter,0))
            iter=model.iter_next(iter)
        return list
    
    def dump_tree(self,treeview):
        list=self.get_charset_list(self.right_list)
        f=open(os.path.join(get_wdir(),"dats/charset_list.dat"),"wb")
        cPickle.dump(list,f,1)
        f.close()
        
    #CODICE PER IL DRAG E DROP
         
    #def data_get(self,treeview,context,selection,info,timestamp):
    #    model,iter=treeview.get_selection().get_selected()
    #    text=model.get_value(iter,0)
    #    if treeview==self.left_list:
    #        selection.set("Charset",8,text)
    #    else:
    #        selection.set("Ord",8,text)
    
    #def data_received_right(self,treeview,context,x,y,selection,info,timestamp):
    #    model=treeview.get_model()
    #    data=selection.data
    #    drop_info=treeview.get_dest_row_at_pos(x,y)
    #    if drop_info:
    #        path,position=drop_info
    #        iter=model.get_iter(path)
    #        if not (data in self.get_charset_list(treeview)) or selection.target=="Ord":
    #            if (position==gtk.TREE_VIEW_DROP_BEFORE or position== gtk.TREE_VIEW_DROP_INTO_OR_BEFORE):
    #                model.insert_before(iter,[data])
    #            else:
    #                model.insert_after(iter,[data])
    #    else:
    #        if not (data in self.get_charset_list(treeview)) or selection.target=="Ord":
    #            model.append([data])
    #    if context.action ==gtk.gdk.ACTION_MOVE:
    #        context.finish(True,True,timestamp)

    #def data_received_left(self,treeview,context,x,y,selection,info,timestamp):
    #    if context.action ==gtk.gdk.ACTION_MOVE:
    #        context.finish(True,True,timestamp)
    
    def set_default_list(self,obj):
        self.right_model.clear()
        for encoding in ordered_list:
            self.right_model.append([encoding])
    
    def add_ordered(self,obj):
        model_left,path_left_list=self.left_list.get_selection().get_selected_rows()
        for path in path_left_list:
            iter_left=model_left.get_iter(path) 
            encoding=model_left.get_value(iter_left,0)
            if not (encoding in self.get_charset_list(self.right_list)):
                self.right_model.append([encoding])

    def remove_ordered(self,obj):
        model_right,path_right_list=self.right_list.get_selection().get_selected_rows()
        for i in range(len(path_right_list)):
            path=path_right_list[i]
            if i!=0:
                path=list(path)
                path[0]=path[0]-i
                path=tuple(path)
            iter_right=model_right.get_iter(path)
            self.right_model.remove(iter_right)
        
    def move_up(self,obj):
        model,path_list=self.right_list.get_selection().get_selected_rows()
        for path in path_list:
            iter_to_move=model.get_iter(path)
            new_path=list(path)
            if new_path[0]>0:
                new_path[0]=new_path[0]-1
            new_path=tuple(new_path)
            iter_previous=model.get_iter(new_path)
            model.swap(iter_to_move,iter_previous)

    def move_down(self,obj):
        model,path_list=self.right_list.get_selection().get_selected_rows()
        path_list.reverse()
        for path in path_list:
            iter_to_move=model.get_iter(path)
            iter_next=model.iter_next(iter_to_move)
            if iter_next!=None:
                model.swap(iter_to_move,iter_next)
   
    def __init__(self):
        self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event",self.delete_event)
        self.window.connect("destroy",self.destroy)
        self.window.set_title("Charset List")
        self.window.set_position(gtk.WIN_POS_CENTER)
        self.window.set_size_request(400,400)
        self.table=gtk.Table(3,2,False)
        self.table.set_border_width(4)
        label_left=gtk.Label()
        label_left.set_markup("<b>"+_("Available Charsets")+"</b>")
        label_right=gtk.Label()
        label_right.set_markup("<b>"+_("Ordered Charsets")+"</b>")
        self.left_scrolledwin=gtk.ScrolledWindow()
        self.left_scrolledwin.set_border_width(4)
        self.left_scrolledwin.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
        self.left_scrolledwin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        self.left_list=gtk.TreeView() 
        self.left_list.set_border_width(4)
        self.left_model=gtk.ListStore(gobject.TYPE_STRING)
        self.left_scrolledwin.add(self.left_list)
        self.left_list.set_model(self.left_model)
        text_renderer=gtk.CellRendererText()
        self.left_column=gtk.TreeViewColumn(_("List"),text_renderer,text=0)
        self.left_list.append_column(self.left_column)
        self.left_list.set_rules_hint(True)
        self.left_list.set_headers_visible(False)
        #self.left_list.enable_model_drag_source(False,[("Charset",gtk.TARGET_SAME_APP,0)],gtk.gdk.ACTION_COPY)
        #self.left_list.enable_model_drag_dest([("Delete",gtk.TARGET_SAME_APP,2)],gtk.gdk.ACTION_MOVE)
        #self.left_list.connect("drag-data-get",self.data_get)
        #self.left_list.connect("drag-data-received",self.data_received_left)
        self.left_list.set_tooltip_text(encodings_tip)
        self.left_list.get_selection().set_mode(gtk.SELECTION_MULTIPLE)


        
        self.right_scrolledwin=gtk.ScrolledWindow()
        self.right_scrolledwin.set_border_width(4)
        self.right_scrolledwin.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
        self.right_scrolledwin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        self.right_list=gtk.TreeView() 
        self.right_list.set_border_width(4)
        self.right_model=gtk.ListStore(gobject.TYPE_STRING)
        self.right_scrolledwin.add(self.right_list)
        self.right_list.set_model(self.right_model)
        text_renderer=gtk.CellRendererText()
        self.right_column=gtk.TreeViewColumn(_("List"),text_renderer,text=0)
        self.right_list.append_column(self.right_column)
        self.right_list.set_rules_hint(True)
        self.right_list.set_headers_visible(False)
        #self.right_list.enable_model_drag_dest([("Charset",gtk.TARGET_SAME_APP,0),("Ord",gtk.TARGET_SAME_WIDGET,1)],gtk.gdk.ACTION_COPY|gtk.gdk.ACTION_MOVE)

        #self.right_list.enable_model_drag_source(False,[("Ord",gtk.TARGET_SAME_WIDGET,1),("Delete",gtk.TARGET_SAME_APP,2)],gtk.gdk.ACTION_MOVE)
        #self.right_list.connect("drag-data-received",self.data_received_right)
        #self.right_list.connect("drag-data-get",self.data_get)
        self.right_list.set_tooltip-text(_("XPN will try to encode your articles with these charsets in this order"))
        self.right_list.set_reorderable(True)
        self.right_list.get_selection().set_mode(gtk.SELECTION_MULTIPLE)


        self.to_right_button=gtk.Button()
        to_right_button_image=gtk.Image()
        to_right_button_image.set_from_stock(gtk.STOCK_GO_FORWARD,gtk.ICON_SIZE_MENU)
        
        self.to_right_button.add(to_right_button_image)
        self.to_right_button.connect("clicked",self.add_ordered)
        self.to_left_button=gtk.Button()
        to_left_button_image=gtk.Image()
        to_left_button_image.set_from_stock(gtk.STOCK_GO_BACK,gtk.ICON_SIZE_MENU)
        
        self.to_left_button.add(to_left_button_image)
        self.to_left_button.connect("clicked",self.remove_ordered)
        vbox_buttons=gtk.VBox()
        vbox_buttons.pack_start(self.to_right_button,False,True,10)
        vbox_buttons.pack_start(self.to_left_button,False,True,10)
        
        up_button_image=gtk.Image()
        up_button_image.set_from_stock(gtk.STOCK_GO_UP,gtk.ICON_SIZE_MENU)
        self.up_button=gtk.Button()
        self.up_button.add(up_button_image)
        self.up_button.connect("clicked",self.move_up)

        down_button_image=gtk.Image()
        down_button_image.set_from_stock(gtk.STOCK_GO_DOWN,gtk.ICON_SIZE_MENU)
        self.down_button=gtk.Button()
        self.down_button.add(down_button_image)
        self.down_button.connect("clicked",self.move_down)

        vbox_buttons2=gtk.VBox()
        vbox_buttons2.pack_start(self.up_button,False,False,10)
        vbox_buttons2.pack_start(self.down_button,False,False,10)
        
        self.default_button=gtk.Button(_("Set Default List"))
        self.default_button.connect("clicked",self.set_default_list)

        self.table.attach(label_left,0,1,0,1,gtk.EXPAND|gtk.FILL,gtk.FILL)
        self.table.attach(vbox_buttons,1,2,1,2,gtk.EXPAND|gtk.FILL,gtk.EXPAND)
        self.table.attach(label_right,2,3,0,1,gtk.EXPAND|gtk.FILL,gtk.FILL)
        self.table.attach(self.left_scrolledwin,0,1,1,2,gtk.EXPAND|gtk.FILL,gtk.EXPAND|gtk.FILL)
        self.table.attach(self.right_scrolledwin,2,3,1,2,gtk.EXPAND|gtk.FILL,gtk.EXPAND|gtk.FILL)
        self.table.attach(vbox_buttons2,3,4,1,2,gtk.FILL,gtk.EXPAND)
        self.table.attach(self.default_button,2,3,2,3,gtk.FILL|gtk.EXPAND,gtk.FILL)

        vbox=gtk.VBox()
        hbox=gtk.HBox()
        self.ok_button=gtk.Button(None,gtk.STOCK_OK)
        self.ok_button.connect("clicked",self.close_ok)
        self.cancel_button=gtk.Button(None,gtk.STOCK_CANCEL)
        self.cancel_button.connect("clicked",self.close_cancel)
        hbox.pack_start(self.cancel_button,True,True,2)
        hbox.pack_start(self.ok_button,True,True,2)
        vbox.pack_start(self.table,True,True,4)
        vbox.pack_start(hbox,False,True,2)
        self.window.add(vbox)
        self.fill_lists()
        self.window.show_all()

if __name__=="__main__":
    ch_list=CharsetList()
    gtk.main()