source: main/trunk/plugins/gedit/openplm.py @ 64

Revision 64, 5.8 KB checked in by pcosquer, 12 years ago (diff)

beginning of gedit plugins

Line 
1# -*- coding: utf-8 -*-
2#  OpenPLM plugin
3
4#  This plugin is based on Color Picker plugin by Jesse van den KieBoom
5#
6#  Copyright (C) 2006 Jesse van den Kieboom,
7#                2010 Pierre Cosquer
8#   
9#  This program is free software; you can redistribute it and/or modify
10#  it under the terms of the GNU General Public License as published by
11#  the Free Software Foundation; either version 2 of the License, or
12#  (at your option) any later version.
13#   
14#  This program is distributed in the hope that it will be useful,
15#  but WITHOUT ANY WARRANTY; without even the implied warranty of
16#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#  GNU General Public License for more details.
18#   
19#  You should have received a copy of the GNU General Public License
20#  along with this program; if not, write to the Free Software
21#  Foundation, Inc., 59 Temple Place, Suite 330,
22#  Boston, MA 02111-1307, USA.
23
24import urllib
25import urllib2
26import gedit, gtk
27import gettext
28from gpdefs import *
29
30try:
31    gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
32    _ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
33except:
34    _ = lambda s: s
35
36ui_str = """
37<ui>
38  <menubar name="MenuBar">
39    <menu name="FileMenu" action="File">
40      <placeholder name="open_plm">
41        <menu name="OpenPLM" action="openplm">
42            <menuitem name="login" action="login"/>
43            <menuitem name="checkout" action="checkout"/>
44         </menu>
45      </placeholder>
46    </menu>
47  </menubar>
48</ui>
49"""
50
51class OpenPLMPluginInstance(object):
52   
53    SERVER = "http://localhost:8000/"
54
55    def __init__(self, plugin, window):
56        self._window = window
57        self._plugin = plugin
58        self._activate_id = 0
59       
60        self.opener = None
61
62        self.insert_menu()
63        self.update()
64
65        self._activate_id = self._window.connect('focus-in-event', \
66                self.on_window_activate)
67
68    def stop(self):
69        self.remove_menu()
70
71        if self._activate_id:
72            self._window.handler_disconnect(self._activate_id)
73
74        self._window = None
75        self._plugin = None
76        self._action_group = None
77        self._activate_id = 0
78
79    def insert_menu(self):
80        manager = self._window.get_ui_manager()
81
82        self._action_group = gtk.ActionGroup("GeditOpenPLMPluginActions")
83        self._action_group.add_actions(
84                [("openplm", None, ("OpenPLM")),
85                    ("login", None, _("Login"), None,
86                _("Login"), lambda a: self.login()),
87                 ("checkout", None, _("Check out"), None,
88                _("Check out"), lambda a: self.login()),
89                 ])
90
91        manager.insert_action_group(self._action_group, -1)
92        self._ui_id = manager.add_ui_from_string(ui_str)
93
94    def remove_menu(self):
95        manager = self._window.get_ui_manager()
96
97        manager.remove_ui(self._ui_id)
98        manager.remove_action_group(self._action_group)
99        manager.ensure_update()
100
101    def update(self):
102        tab = self._window.get_active_tab()
103        self._action_group.set_sensitive(tab != None)
104
105        if not tab and self._plugin._dialog and \
106                self._plugin._dialog.get_transient_for() == self._window:
107            self._plugin._dialog.response(gtk.RESPONSE_CLOSE)
108
109    def on_window_activate(self, window, event):
110        self._plugin.dialog_transient_for(window)
111
112    def login(self):
113        diag = LoginDialog(self._window)
114        resp = diag.run()
115        if resp == gtk.RESPONSE_ACCEPT:
116            username = diag.get_username()
117            password = diag.get_password()
118            auth_handler = urllib2.HTTPBasicAuthHandler()
119            auth_handler.add_password(realm="", uri=self.SERVER ,
120                                      user=username,
121                                      passwd=password)
122
123            self.opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
124                                              urllib2.HTTPCookieProcessor())
125            data = urllib.urlencode(dict(username=username, password=password,
126                                         next="home/"))
127            f = self.opener.open(self.SERVER + "login/", data)
128
129        diag.destroy()
130
131
132class LoginDialog(gtk.Dialog):
133   
134    def __init__(self, window):
135        super(LoginDialog, self).__init__(_("Login"), window,
136                                         gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
137                                         (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
138                                          gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
139        table = gtk.Table(2, 2)
140        self.vbox.pack_start(table)
141        table.attach(gtk.Label(_("Username")), 0, 1, 0, 1)
142        table.attach(gtk.Label(_("Password")), 0, 1, 1, 2)
143        self.user_entry = gtk.Entry()
144        table.attach(self.user_entry, 1, 2, 0, 1)
145        self.pw_entry = gtk.Entry()
146        self.pw_entry.set_visibility(False)
147        table.attach(self.pw_entry, 1, 2, 1, 2)
148        table.show_all()
149
150    def get_username(self):
151        return self.user_entry.get_text()
152
153    def get_password(self):
154        return self.pw_entry.get_text()
155
156
157class OpenPLMPlugin(gedit.Plugin):
158    DATA_TAG = "OpenPLMPluginInstance"
159
160    def __init__(self):
161        gedit.Plugin.__init__(self)
162        self._dialog = None
163
164    def get_instance(self, window):
165        return window.get_data(self.DATA_TAG)
166
167    def set_instance(self, window, instance):
168        window.set_data(self.DATA_TAG, instance)
169
170    def activate(self, window):
171        self.set_instance(window, OpenPLMPluginInstance(self, window))
172
173    def deactivate(self, window):
174        self.get_instance(window).stop()
175        self.set_instance(window, None)
176
177    def update_ui(self, window):
178        self.get_instance(window).update()
179
180    def dialog_transient_for(self, window):
181        if self._dialog:
182            self._dialog.set_transient_for(window)
183
184
Note: See TracBrowser for help on using the repository browser.