This file is indexed.

/usr/share/pyshared/chirpui/importdialog.py is in chirp 0.1.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/python
#
# Copyright 2008 Dan Smith <dsmith@danplanet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import gobject
import pango

from chirp import errors, chirp_common, xml, import_logic
from chirpui import common

class WaitWindow(gtk.Window):
    def __init__(self, msg, parent=None):
        gtk.Window.__init__(self)
        self.set_title("Please Wait")
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        if parent:
            self.set_transient_for(parent)
            self.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        else:
            self.set_position(gtk.WIN_POS_CENTER)

        vbox = gtk.VBox(False, 2)

        l = gtk.Label(msg)
        l.show()
        vbox.pack_start(l)

        self.prog = gtk.ProgressBar()
        self.prog.show()
        vbox.pack_start(self.prog)

        vbox.show()
        self.add(vbox)

    def grind(self):
        while gtk.events_pending():
            gtk.main_iteration(False)

        self.prog.pulse()

    def set(self, fraction):
        while gtk.events_pending():
            gtk.main_iteration(False)

        self.prog.set_fraction(fraction)

class ImportDialog(gtk.Dialog):

    def _check_for_dupe(self, location):
        iter = self.__store.get_iter_first()
        while iter:
            imp, loc = self.__store.get(iter, self.col_import, self.col_nloc)
            if imp and loc == location:
                return True
            iter = self.__store.iter_next(iter)

        return False

    def _toggle(self, rend, path, col):
        iter = self.__store.get_iter(path)
        imp, nloc = self.__store.get(iter, self.col_import, self.col_nloc)
        if not imp and self._check_for_dupe(nloc):
            d = gtk.MessageDialog(parent=self, buttons=gtk.BUTTONS_OK)
            d.set_property("text",
                           "Location %i is already being imported.  " % nloc + \
                               "Choose another value for 'New Location' " + \
                               "before selecting 'Import'")
            d.run()
            d.destroy()
        else:
            self.__store[path][col] = not imp

    def _render(self, _, rend, model, iter, colnum):
        newloc, imp = model.get(iter, self.col_nloc, self.col_import)
        lo,hi = self.dst_radio.get_features().memory_bounds

        rend.set_property("text", "%i" % newloc)
        if newloc in self.used_list and imp:
            rend.set_property("foreground", "goldenrod")
            rend.set_property("weight", pango.WEIGHT_BOLD)
        elif newloc < lo or newloc > hi:
            rend.set_property("foreground", "red")
            rend.set_property("weight", pango.WEIGHT_BOLD)
        else:
            rend.set_property("foreground", "black")
            rend.set_property("weight", pango.WEIGHT_NORMAL)

    def _edited(self, rend, path, new, col):
        iter = self.__store.get_iter(path)
        nloc, = self.__store.get(iter, self.col_nloc)

        try:
            val = int(new)
        except ValueError:
            common.show_error("Invalid value.  Must be an integer.")
            return

        if val == nloc:
            return

        if self._check_for_dupe(val):
            d = gtk.MessageDialog(parent=self, buttons=gtk.BUTTONS_OK)
            d.set_property("text",
                           "Location %i is already being imported" % val)
            d.run()
            d.destroy()
            return

        self.record_use_of(val)

        self.__store.set(iter, col, val)

    def get_import_list(self):
        import_list = []
        iter = self.__store.get_iter_first()
        while iter:
            old, new, enb = self.__store.get(iter,
                                             self.col_oloc,
                                             self.col_nloc,
                                             self.col_import)
            if enb:
                import_list.append((old, new))
            iter = self.__store.iter_next(iter)

        return import_list

    def ensure_calls(self, dst_rthread, import_list):
        rlist_changed = False
        ulist_changed = False

        if not isinstance(self.dst_radio, chirp_common.IcomDstarSupport):
            return

        ulist = self.dst_radio.get_urcall_list()
        rlist = self.dst_radio.get_repeater_call_list()

        for old, new in import_list:
            mem = self.src_radio.get_memory(old)
            if isinstance(mem, chirp_common.DVMemory):
                if mem.dv_urcall not in ulist:
                    print "Adding %s to ucall list" % mem.dv_urcall
                    ulist.append(mem.dv_urcall)
                    ulist_changed = True
                if mem.dv_rpt1call not in rlist:
                    print "Adding %s to rcall list" % mem.dv_rpt1call
                    rlist.append(mem.dv_rpt1call)
                    rlist_changed = True
                if mem.dv_rpt2call not in rlist:
                    print "Adding %s to rcall list" % mem.dv_rpt2call
                    rlist.append(mem.dv_rpt2call)
                    rlist_changed = True
                
        if ulist_changed:
            job = common.RadioJob(None, "set_urcall_list", ulist)
            job.set_desc("Updating URCALL list")
            dst_rthread._qsubmit(job, 0)

        if rlist_changed:
            job = common.RadioJob(None, "set_repeater_call_list", ulist)
            job.set_desc("Updating RPTCALL list")
            dst_rthread._qsubmit(job, 0)
            
        return

    def _convert_power(self, dst_levels, src_levels, mem):
        if not dst_levels:
            mem.power = None
            return
        elif not mem.power:
            # Source radio does not support power levels, so choose the
            # first (highest) level from the destination radio.
            mem.power = dst_levels[0]
            return ""

        # If both radios support power levels, we need to decide how to
        # convert the source power level to a valid one for the destination
        # radio.  To do that, find the absolute level of the source value
        # and calculate the different between it and all the levels of the
        # destination, choosing the one that matches most closely.

        deltas = [abs(mem.power - power) for power in dst_levels]
        mem.power = dst_levels[deltas.index(min(deltas))]

    def do_soft_conversions(self, dst_features, src_features, mem):
        self._convert_power(dst_features.valid_power_levels,
                            src_features.valid_power_levels,
                            mem)

        return mem

    def do_import_banks(self):
        try:
            dst_banks = self.dst_radio.get_banks()
            src_banks = self.src_radio.get_banks()
            if not dst_banks or not src_banks:
                raise Exception()
        except Exception:
            print "One or more of the radios doesn't support banks"
            return

        if not isinstance(self.dst_radio, xml.XMLRadio) and \
                len(dst_banks) != len(src_banks):
            print "Source and destination radios have a different number of banks"
        else:
            self.dst_radio.set_banks(src_banks)

    def do_import(self, dst_rthread):
        i = 0
        error_messages = {}
        import_list = self.get_import_list()

        for old, new in import_list:
            i += 1
            print "%sing %i -> %i" % (self.ACTION, old, new)

            src = self.src_radio.get_memory(old)

            try:
                mem = import_logic.import_mem(self.dst_radio,
                                              self.src_radio,
                                              src,
                                             {"number" : new})
            except import_logic.ImportError, e:
                print e
                error_messages[new] = str(e)
                continue

            job = common.RadioJob(None, "set_memory", mem)
            job.set_desc("Setting memory %i" % mem.number)
            dst_rthread._qsubmit(job, 0)

        if error_messages.keys():
            msg = "Error importing memories:\r\n"
            for num, msgs in error_messages.items():
                msg += "%s: %s" % (num, ",".join(msgs))
            common.show_error(msg)

        return i

    def make_view(self):
        editable = [self.col_nloc]

        self.__store = gtk.ListStore(gobject.TYPE_BOOLEAN, # Import
                                     gobject.TYPE_INT,     # Source loc
                                     gobject.TYPE_INT,     # Destination loc
                                     gobject.TYPE_STRING,  # Name
                                     gobject.TYPE_STRING,  # Frequency
                                     gobject.TYPE_STRING,  # Comment
                                     gobject.TYPE_BOOLEAN,
                                     gobject.TYPE_STRING)
        self.__view = gtk.TreeView(self.__store)
        self.__view.show()

        for k in self.caps.keys():
            t = self.types[k]

            if t == gobject.TYPE_BOOLEAN:
                rend = gtk.CellRendererToggle()
                rend.connect("toggled", self._toggle, k)
                column = gtk.TreeViewColumn(self.caps[k], rend,
                                            active=k,
                                            sensitive=self.col_okay,
                                            activatable=self.col_okay)
            else:
                rend = gtk.CellRendererText()
                if k in editable:
                    rend.set_property("editable", True)
                    rend.connect("edited", self._edited, k)
                column = gtk.TreeViewColumn(self.caps[k], rend,
                                            text=k,
                                            sensitive=self.col_okay)

            if k == self.col_nloc:
                column.set_cell_data_func(rend, self._render, k)

            column.set_sort_column_id(k)
            self.__view.append_column(column)
        
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.add(self.__view)
        sw.show()

        return sw

    def __select_all(self, button, state):
        iter = self.__store.get_iter_first()
        while iter:
            _state, okay, = self.__store.get(iter,
                                             self.col_import,
                                             self.col_okay)
            if state is None:
                _state = not _state and okay
            else:
                _state = state and okay
            self.__store.set(iter, self.col_import, _state)
            iter = self.__store.iter_next(iter)

    def __incrnew(self, button, delta):
        iter = self.__store.get_iter_first()
        while iter:
            pos = self.__store.get(iter, self.col_nloc)[0]
            pos += delta
            if pos < 0:
                pos = 0
            self.__store.set(iter, self.col_nloc, pos)
            iter = self.__store.iter_next(iter)

    def __autonew(self, button):
        pos = self.dst_radio.get_features().memory_bounds[0]
        iter = self.__store.get_iter_first()
        while iter:
            selected, okay = self.__store.get(iter,
                                              self.col_import, self.col_okay)
            if selected and okay:
                self.__store.set(iter, self.col_nloc, pos)
                pos += 1
            iter = self.__store.iter_next(iter)

    def __revrnew(self, button):
        positions = []
        iter = self.__store.get_iter_first()
        while iter:
            positions.append(self.__store.get(iter, self.col_nloc)[0])
            iter = self.__store.iter_next(iter)

        iter = self.__store.get_iter_first()
        while iter:
            self.__store.set(iter, self.col_nloc, positions.pop())
            iter = self.__store.iter_next(iter)

    def make_select(self):
        hbox = gtk.HBox(True, 2)

        all = gtk.Button("All");
        all.connect("clicked", self.__select_all, True)
        all.set_size_request(50, 25)
        all.show()
        hbox.pack_start(all, 0, 0, 0)

        none = gtk.Button("None");
        none.connect("clicked", self.__select_all, False)
        none.set_size_request(50, 25)
        none.show()
        hbox.pack_start(none, 0, 0, 0)

        inv = gtk.Button("Inverse")
        inv.connect("clicked", self.__select_all, None)
        inv.set_size_request(50, 25)
        inv.show()
        hbox.pack_start(inv, 0, 0, 0)

        frame = gtk.Frame("Select")
        frame.show()
        frame.add(hbox)
        hbox.show()

        return frame

    def make_adjust(self):
        hbox = gtk.HBox(True, 2)

        incr = gtk.Button("+1")
        incr.connect("clicked", self.__incrnew, 1)
        incr.set_size_request(50, 25)
        incr.show()
        hbox.pack_start(incr, 0, 0, 0)

        decr = gtk.Button("-1")
        decr.connect("clicked", self.__incrnew, -1)
        decr.set_size_request(50, 25)
        decr.show()
        hbox.pack_start(decr, 0, 0, 0)

        auto = gtk.Button("Auto")
        auto.connect("clicked", self.__autonew)
        auto.set_size_request(50, 25)
        auto.show()
        hbox.pack_start(auto, 0, 0, 0)

        revr = gtk.Button("Reverse")
        revr.connect("clicked", self.__revrnew)
        revr.set_size_request(50, 25)
        revr.show()
        hbox.pack_start(revr, 0, 0, 0)

        frame = gtk.Frame("Adjust New Location")
        frame.show()
        frame.add(hbox)
        hbox.show()

        return frame

    def make_options(self):
        hbox = gtk.HBox(True, 2)

        confirm = gtk.CheckButton("Confirm overwrites")
        confirm.connect("toggled", __set_confirm)
        confirm.show()

        hbox.pack_start(confirm, 0, 0, 0)

        frame = gtk.Frame("Options")
        frame.add(hbox)
        frame.show()
        hbox.show()

        return frame

    def make_controls(self):
        hbox = gtk.HBox(True, 2)
        
        hbox.pack_start(self.make_select(), 0, 0, 0)
        hbox.pack_start(self.make_adjust(), 0, 0, 0)
        #hbox.pack_start(self.make_options(), 0, 0, 0)
        hbox.show()

        return hbox

    def build_ui(self):
        self.vbox.pack_start(self.make_view(), 1, 1, 1)
        self.vbox.pack_start(self.make_controls(), 0, 0, 0)

    def record_use_of(self, number):
        lo, hi = self.dst_radio.get_features().memory_bounds

        if number < lo or number > hi:
            return

        try:
            mem = self.dst_radio.get_memory(number)
            if mem and not mem.empty and number not in self.used_list:
                self.used_list.append(number)
        except errors.InvalidMemoryLocation:
            print "Location %i empty or at limit of destination radio" % number
        except errors.InvalidDataError, e:
            print "Got error from radio, assuming %i beyond limits: %s" % \
                (number, e)

    def populate_list(self):
        start, end = self.src_radio.get_features().memory_bounds
        for i in range(start, end+1):
            self.ww.set(float(i) / end)
            try:
                mem = self.src_radio.get_memory(i)
            except errors.InvalidMemoryLocation, e:
                continue
            if mem.empty:
                continue

            msgs = self.dst_radio.validate_memory(mem)
            errs = [x for x in msgs if isinstance(x, chirp_common.ValidationError)]
            if errs:
                msg = "Unsupported by destination radio: %s" % (",".join(msgs))
            else:
                errs = []
                msg = str(mem)

            self.__store.append(row=(not bool(msgs),
                                     mem.number,
                                     mem.number,
                                     mem.name,
                                     chirp_common.format_freq(mem.freq),
                                     mem.comment,
                                     not bool(errs),
                                     msg
                                     ))
            self.record_use_of(mem.number)


    TITLE = "Import From File"
    ACTION = "Import"

    def __init__(self, src_radio, dst_radio, parent=None):
        gtk.Dialog.__init__(self,
                            buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
                                     gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
                            title=self.TITLE,
                            parent=parent)

        self.col_import = 0
        self.col_nloc = 1
        self.col_oloc = 2
        self.col_name = 3
        self.col_freq = 4
        self.col_comm = 5
        self.col_okay = 6
        self.col_tmsg = 7

        self.caps = {
            self.col_import : self.ACTION,
            self.col_nloc   : "New location",
            self.col_oloc   : "Location",
            self.col_name   : "Name",
            self.col_freq   : "Frequency",
            self.col_comm   : "",
            }

        self.types = {
            self.col_import : gobject.TYPE_BOOLEAN,
            self.col_oloc   : gobject.TYPE_INT,
            self.col_nloc   : gobject.TYPE_INT,
            self.col_name   : gobject.TYPE_STRING,
            self.col_freq   : gobject.TYPE_STRING,
            self.col_comm   : gobject.TYPE_STRING,
            self.col_okay   : gobject.TYPE_BOOLEAN,
            self.col_tmsg   : gobject.TYPE_STRING,
            }

        self.src_radio = src_radio
        self.dst_radio = dst_radio

        self.used_list = []
        self.not_used_list = []

        self.build_ui()
        self.set_default_size(400, 300)

        self.ww = WaitWindow("Preparing memory list...", parent=parent)
        self.ww.show()
        self.ww.grind()

        self.populate_list()

        self.ww.hide()

class ExportDialog(ImportDialog):
    TITLE = "Export To File"
    ACTION = "Export"

if __name__ == "__main__":
    from chirpui import editorset
    import sys

    f = sys.argv[1]
    rc = editorset.radio_class_from_file(f)
    radio = rc(f)

    d = ImportDialog(radio)
    d.run()

    print d.get_import_list()