~parinporecha/gtg/config_parser_bug

« back to all changes in this revision

Viewing changes to GTG/plugins/geolocalized_tasks/geolocalized_tasks.py

  • Committer: Izidor Matušov
  • Date: 2013-02-25 07:35:07 UTC
  • Revision ID: izidor.matusov@gmail.com-20130225073507-vgts69uthx7z2run
PEP8ification by Nimit

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
        self.menu_item = gtk.MenuItem("Geolocalized-tasks Preferences")
40
40
 
41
41
        self.PROXIMITY_FACTOR = 5  # 5 km
42
 
        self.LOCATION_DETERMINATION_METHOD = [] # "network", "gps", "cellphone"
 
42
        self.LOCATION_DETERMINATION_METHOD = []
 
43
            # "network", "gps", "cellphone"
43
44
 
44
45
        for provider in self.geoclue.get_available_providers():
45
46
            status = self.geoclue.provider_status(provider['object'])
63
64
        # create the pixbuf with the icon and it's size.
64
65
        # 24,24 is the TaskEditor's toolbar icon size
65
66
        image_assign_location_path = os.path.join(self.plugin_path,
66
 
                                    "icons/hicolor/16x16/assign-location.png")
 
67
                                                  "icons/hicolor/16x16/assign\
 
68
                                                  -location.png")
67
69
        pixbug_assign_location = gtk.gdk.pixbuf_new_from_file_at_size(
68
 
                                    image_assign_location_path, 16, 16)
 
70
            image_assign_location_path, 16, 16)
69
71
 
70
72
        image_assign_location = gtk.Image()
71
73
        image_assign_location.set_from_pixbuf(pixbug_assign_location)
77
79
        # TODO: add a short cut to the menu
78
80
 
79
81
        self.context_item.connect('activate',
80
 
                    self.on_contextmenu_tag_location, plugin_api)
 
82
                                  self.on_contextmenu_tag_location, plugin_api)
81
83
        plugin_api.add_menu_tagpopup(self.context_item)
82
84
 
83
85
        # get the user settings from the config file
88
90
                self.PROXIMITY_FACTOR = value
89
91
 
90
92
            if "location_determination_method" in\
91
 
                self.config["geolocalized-tasks"]:
 
93
                    self.config["geolocalized-tasks"]:
92
94
                self.LOCATION_DETERMINATION_METHOD =\
93
 
                    self.config["geolocalized-tasks"]\
94
 
                    ["location_determination_method"]
 
95
                    self.config["geolocalized-tasks"]["location_determination\
 
96
                                                                     _method"]
95
97
 
96
98
        providers = self.geoclue.get_available_providers()
97
99
        provider_name_list = []
106
108
                    for provider in providers:
107
109
                        if provider['name'].lower() == "hostip":
108
110
                            status = self.geoclue.provider_status(
109
 
                                                    provider['object'])
 
111
                                provider['object'])
110
112
                            if status in ["error", "unavailable"]:
111
113
                                if "network" in\
112
 
                                    self.LOCATION_DETERMINATION_METHOD:
 
114
                                        self.LOCATION_DETERMINATION_METHOD:
113
115
                                    self.LOCATION_DETERMINATION_METHOD.remove(
114
 
                                                            "network")
 
116
                                        "network")
115
117
                                    break
116
118
                else:
117
119
                    self.LOCATION_DETERMINATION_METHOD.remove("network")
118
120
            elif method == "gps":
119
121
                if "gpsd" in provider_name_list or\
120
 
                    "gypsy" in provider_name_list:
 
122
                        "gypsy" in provider_name_list:
121
123
                    for provider in providers:
122
124
                        if provider['name'].lower() in ["gpsd", "gypsy"]:
123
125
                            status = self.geoclue.provider_status(
124
 
                                                    provider['object'])
 
126
                                provider['object'])
125
127
                            if status in ["error", "unavailable"]:
126
128
                                if "gps" in self.LOCATION_DETERMINATION_METHOD:
127
129
                                    self.LOCATION_DETERMINATION_METHOD.remove(
128
 
                                                                        "gps")
 
130
                                        "gps")
129
131
                                    break
130
132
                else:
131
133
                    self.LOCATION_DETERMINATION_METHOD.remove("gps")
134
136
                    for provider in providers:
135
137
                        if provider['name'].lower() == "gsmloc":
136
138
                            status = self.geoclue.provider_status(
137
 
                                                    provider['object'])
 
139
                                provider['object'])
138
140
                            if status in ["error", "unavailable"]:
139
141
                                if "cellphone" in\
140
 
                                    self.LOCATION_DETERMINATION_METHOD:
 
142
                                        self.LOCATION_DETERMINATION_METHOD:
141
143
                                    self.LOCATION_DETERMINATION_METHOD.remove(
142
 
                                                        "cellphone")
 
144
                                        "cellphone")
143
145
                                    break
144
146
                else:
145
147
                    self.LOCATION_DETERMINATION_METHOD.remove("cellphone")
146
148
 
147
149
        try:
148
150
            if len(self.LOCATION_DETERMINATION_METHOD) == 1 and\
149
 
            "network" in self.LOCATION_DETERMINATION_METHOD:
 
151
                    "network" in self.LOCATION_DETERMINATION_METHOD:
150
152
                self.geoclue.init()
151
153
            elif len(self.LOCATION_DETERMINATION_METHOD) == 1 and\
152
 
            "cellphone" in self.LOCATION_DETERMINATION_METHOD:
 
154
                    "cellphone" in self.LOCATION_DETERMINATION_METHOD:
153
155
                self.geoclue.init(resource=(1 << 1))
154
156
            elif len(self.LOCATION_DETERMINATION_METHOD) == 1 and\
155
 
            "gps" in self.LOCATION_DETERMINATION_METHOD:
 
157
                    "gps" in self.LOCATION_DETERMINATION_METHOD:
156
158
                self.geoclue.init(resource=(1 << 2))
157
159
            else:
158
160
                self.geoclue.init(resource=((1 << 10) - 1))
176
178
                self.config["geolocalized-tasks"] = {
177
179
                    "proximity_factor": self.PROXIMITY_FACTOR,
178
180
                    "location_determination_method":
179
 
                        self.LOCATION_DETERMINATION_METHOD,
 
181
                    self.LOCATION_DETERMINATION_METHOD,
180
182
                }
181
183
        except:
182
184
            pass
188
190
        # unregister the filter callback
189
191
        plugin_api.unregister_filter_cb(self.task_location_filter)
190
192
 
191
 
        #remove the toolbar buttons
 
193
        # remove the toolbar buttons
192
194
        plugin_api.remove_task_toolbar_item(self.task_separator)
193
195
        plugin_api.remove_task_toolbar_item(self.btn_set_location)
194
196
 
195
197
    def onTaskOpened(self, plugin_api):
196
198
        image_geolocalization_path = os.path.join(self.plugin_path,
197
 
                           "icons/hicolor/24x24/geolocalization.png")
 
199
                                                  "icons/hicolor/24x24/\
 
200
                                                  geolocalization.png")
198
201
        pixbuf_geolocalization = gtk.gdk.pixbuf_new_from_file_at_size(
199
 
                            image_geolocalization_path, 24, 24)
 
202
            image_geolocalization_path, 24, 24)
200
203
 
201
204
        # create the image and associate the pixbuf
202
205
        icon_geolocalization = gtk.Image()
209
212
        btn_location_view.set_label("Location View")
210
213
 
211
214
        self.task_separator = plugin_api.add_task_toolbar_item(
212
 
                            gtk.SeparatorToolItem())
 
215
            gtk.SeparatorToolItem())
213
216
 
214
217
        btn_set_location = gtk.ToolButton()
215
218
        btn_set_location.set_icon_widget(icon_geolocalization)
216
219
        btn_set_location.set_label("Set/View location")
217
220
        btn_set_location.connect('clicked', self.set_task_location, plugin_api)
218
221
        self.btn_set_location = plugin_api.add_task_toolbar_item(
219
 
                            btn_set_location)
 
222
            btn_set_location)
220
223
 
221
224
    def is_configurable(self):
222
225
        return True
239
242
            if task.is_workable():
240
243
                tags = task.get_tags()
241
244
 
242
 
                #check if it has the location set
 
245
                # check if it has the location set
243
246
                for tag in tags:
244
247
                    if "location" in tag.get_all_attributes():
245
248
                        has_location = True
258
261
                                    return False
259
262
        return True
260
263
 
261
 
 
262
 
    #=== GEOLOCALIZED PREFERENCES==============================================
 
264
    #=== GEOLOCALIZED PREFERENCES=============================================
263
265
    def on_geolocalized_preferences(self):
264
266
        wTree = gtk.glade.XML(self.glade_file, "Preferences")
265
267
        dialog = wTree.get_widget("Preferences")
294
296
                for provider in providers:
295
297
                    status = self.geoclue.provider_status(provider['object'])
296
298
                    if provider['name'].lower() == "hostip" and\
297
 
                        status in ["error", "unavailable"]:
 
299
                            status in ["error", "unavailable"]:
298
300
                        check_network.set_active(False)
299
301
                        check_network.set_sensitive(False)
300
302
                        break
317
319
                for provider in providers:
318
320
                    status = self.geoclue.provider_status(provider['object'])
319
321
                    if provider['name'].lower() == "gsmloc" and\
320
 
                        status in ["error", "unavailable"]:
 
322
                            status in ["error", "unavailable"]:
321
323
                        check_cellphone.set_active(False)
322
324
                        check_cellphone.set_sensitive(False)
323
325
                        break
342
344
                for provider in providers:
343
345
                    status = self.geoclue.provider_status(provider['object'])
344
346
                    if provider['name'].lower() in ["gpsd", "gypsy"] and\
345
 
                        status in ["error", "unavailable"]:
 
347
                            status in ["error", "unavailable"]:
346
348
                        check_gps.set_active(False)
347
349
                        check_gps.set_sensitive(False)
348
350
                        break
350
352
        spin_proximityfactor = wTree.get_widget("spin_proximityfactor")
351
353
        spin_proximityfactor.set_value(float(self.PROXIMITY_FACTOR))
352
354
        spin_proximityfactor.connect("changed",
353
 
                                self.spin_proximityfactor_changed)
 
355
                                     self.spin_proximityfactor_changed)
354
356
        self.tmp_proximityfactor = float(self.PROXIMITY_FACTOR)
355
357
 
356
358
        dialog.show_all()
393
395
 
394
396
        champlain_view = champlain.View()
395
397
        champlain_view.set_property("scroll-mode",
396
 
                        champlain.SCROLL_MODE_KINETIC)
397
 
        #champlain_view.set_property("zoom-on-double-click", False)
 
398
                                    champlain.SCROLL_MODE_KINETIC)
 
399
        # champlain_view.set_property("zoom-on-double-click", False)
398
400
 
399
401
        # create a list of the tags and their attributes
400
402
        tag_list = []
443
445
            try:
444
446
                if self.location['longitude'] and self.location['latitude']:
445
447
                    self.marker_list.append(layer.add_marker(
446
 
                            plugin_api.get_task_title(),
447
 
                            self.location['latitude'],
448
 
                            self.location['longitude']))
 
448
                        plugin_api.get_task_title(),
 
449
                        self.location['latitude'],
 
450
                        self.location['longitude']))
449
451
            except:
450
452
                self.marker_list.append(layer.add_marker(
451
 
                        plugin_api.get_task_title(), None, None))
 
453
                    plugin_api.get_task_title(), None, None))
452
454
 
453
455
        champlain_view.add_layer(layer)
454
456
 
459
461
            # method that will change the marker's position
460
462
            champlain_view.set_reactive(True)
461
463
            champlain_view.connect("button-release-event",
462
 
                           self.champlain_change_marker, champlain_view)
 
464
                                   self.champlain_change_marker,
 
465
                                   champlain_view)
463
466
 
464
467
        layer.show_all()
465
468
 
489
492
            dialog_action_area_btn.remove(btn_close)
490
493
            # show a close button or the ok/cancel
491
494
            dialog.connect("response", self.set_task_location_close,
492
 
                                                        plugin_api)
 
495
                           plugin_api)
493
496
 
494
 
        #if there is no location set, we want to set it
 
497
        # if there is no location set, we want to set it
495
498
        if not task_has_location:
496
499
            self.location_defined = False
497
500
            if len(plugin_api.get_tags()) > 0:
502
505
                self.cmb_existing_tag.set_text_column(0)
503
506
                self.cmb_existing_tag.set_active(0)
504
507
            else:
505
 
                #remove radiobutton2 and the comboboxentry
 
508
                # remove radiobutton2 and the comboboxentry
506
509
                tabela.remove(self.radiobutton1)
507
510
                tabela.remove(self.radiobutton2)
508
511
                tabela.remove(self.cmb_existing_tag)
525
528
            try:
526
529
                if self.location['longitude'] and self.location['latitude']:
527
530
                    champlain_view.center_on(self.location['latitude'],
528
 
                                self.location['longitude'])
 
531
                                             self.location['longitude'])
529
532
            except:
530
533
                pass
531
534
 
547
550
                        # because users sometimes make mistakes,
548
551
                        # I'll check if the tag exists
549
552
                        tmp_tag = ""
550
 
                        tag_name = self.txt_new_tag.get_text().replace("@", "")
 
553
                        tag_name = self.txt_new_tag.get_text().replace("@",
 
554
                                                                       "")
551
555
                        tag_name = "@" + tag_name
552
556
                        for tag in plugin_api.get_tags():
553
557
                            if tag.get_attribute("name") == tag_name:
554
558
                                tmp_tag = tag_name
555
559
                        if tmp_tag:
556
560
                            plugin_api.add_tag_attribute(tag_name,
557
 
                                    "location", marker_position)
 
561
                                                         "location",
 
562
                                                         marker_position)
558
563
                        else:
559
564
                            plugin_api.insert_tag(tag_name[1:])
560
565
                            plugin_api.add_tag_attribute(tag_name,
561
 
                                    "location", marker_position)
 
566
                                                         "location",
 
567
                                                         marker_position)
562
568
                        dialog.destroy()
563
569
                    else:
564
570
                        # does nothing, no tag set.
571
577
                    index = self.cmb_existing_tag.get_active()
572
578
                    model = self.cmb_existing_tag.get_model()
573
579
                    plugin_api.add_tag_attribute(model[index][0],
574
 
                                        "location", marker_position)
 
580
                                                 "location", marker_position)
575
581
                    dialog.destroy()
576
582
        else:
577
583
            # cancel
613
619
 
614
620
        champlain_view = champlain.View()
615
621
        champlain_view.set_property("scroll-mode",
616
 
                        champlain.SCROLL_MODE_KINETIC)
 
622
                                    champlain.SCROLL_MODE_KINETIC)
617
623
 
618
624
        layer = MarkerLayer()
619
625
 
620
626
        marker_tag = None
621
627
        if tag_location:
622
628
            marker_tag = layer.add_marker(tag.get_attribute("name"),
623
 
                tag_location[0], tag_location[1], tag_color)
 
629
                                          tag_location[0], tag_location[1],
 
630
                                          tag_color)
624
631
        else:
625
632
            try:
626
633
                if self.location['longitude'] and self.location['latitude']:
627
634
                    marker_tag = layer.add_marker(tag.get_attribute("name"),
628
 
                        self.location['latitude'], self.location['longitude'],
629
 
                        tag_color)
 
635
                                                  self.location['latitude'],
 
636
                                                  self.location['longitude'],
 
637
                                                  tag_color)
630
638
            except:
631
639
                marker_tag = layer.add_marker(tag.get_attribute("name"),
632
 
                                                None, None)
 
640
                                              None, None)
633
641
 
634
642
        champlain_view.add_layer(layer)
635
643
 
638
646
 
639
647
        champlain_view.set_reactive(True)
640
648
        champlain_view.connect("button-release-event",
641
 
            self.champlain__tag_change_marker, champlain_view, marker_tag)
 
649
                               self.champlain__tag_change_marker,
 
650
                               champlain_view, marker_tag)
642
651
 
643
652
        layer.show_all()
644
653
 
671
680
            try:
672
681
                if self.location['longitude'] and self.location['latitude']:
673
682
                    champlain_view.center_on(self.location['latitude'],
674
 
                                            self.location['longitude'])
 
683
                                             self.location['longitude'])
675
684
            except:
676
685
                pass
677
686