| | 2056 | class RosterItemExchangeWindow: |
| | 2057 | ''' Windows used when someone send you a exchange contact suggestion ''' |
| | 2058 | def __init__(self, account, action, exchange_list, jid_from, message_body): |
| | 2059 | self.account = account |
| | 2060 | self.action = action |
| | 2061 | self.exchange_list = exchange_list |
| | 2062 | self.message_body = message_body |
| | 2063 | self.jid_from = jid_from |
| | 2064 | |
| | 2065 | # Connect to glade |
| | 2066 | self.xml = gtkgui_helpers.get_glade('roster_item_exchange_window.glade') |
| | 2067 | self.window = self.xml.get_widget('roster_item_exchange_window') |
| | 2068 | |
| | 2069 | # Add Widgets. |
| | 2070 | for widget_to_add in ['cancel_button', 'accept_button', 'type_label', |
| | 2071 | 'body_scrolledwidow', 'body_textview', 'items_list_treeview']: |
| | 2072 | self.__dict__[widget_to_add] = self.xml.get_widget(widget_to_add) |
| | 2073 | |
| | 2074 | # Set labels |
| | 2075 | #self.action can be 'add', 'modify' or 'remove' |
| | 2076 | self.type_label.set_label(\ |
| | 2077 | _('<b>%s</b> would like you to <b>%s</b> some contacts in your roster.') \ |
| | 2078 | % (jid_from, _(self.action))) |
| | 2079 | if message_body: |
| | 2080 | buffer = self.body_textview.get_buffer() |
| | 2081 | buffer.set_text(self.message_body) |
| | 2082 | else: |
| | 2083 | self.body_scrolledwindow.hide() |
| | 2084 | # Treeview |
| | 2085 | model = gtk.ListStore(bool, str, str, str, str) |
| | 2086 | self.items_list_treeview.set_model(model) |
| | 2087 | # columns |
| | 2088 | renderer1 = gtk.CellRendererToggle() |
| | 2089 | renderer1.set_property('activatable', True) |
| | 2090 | renderer1.connect('toggled', self.toggled_callback) |
| | 2091 | self.items_list_treeview.insert_column_with_attributes(-1, |
| | 2092 | _(self.action), renderer1, active = 0) |
| | 2093 | renderer2 = gtk.CellRendererText() |
| | 2094 | self.items_list_treeview.insert_column_with_attributes(-1, |
| | 2095 | _('Jabber ID'), renderer2, text = 1) |
| | 2096 | renderer3 = gtk.CellRendererText() |
| | 2097 | self.items_list_treeview.insert_column_with_attributes(-1, |
| | 2098 | _('Name'), renderer3, text = 2) |
| | 2099 | renderer4 = gtk.CellRendererText() |
| | 2100 | self.items_list_treeview.insert_column_with_attributes(-1, |
| | 2101 | _('Groups'), renderer4, text = 3) |
| | 2102 | |
| | 2103 | # Init contacts |
| | 2104 | model = self.items_list_treeview.get_model() |
| | 2105 | model.clear() |
| | 2106 | |
| | 2107 | if action == 'add': |
| | 2108 | for jid in self.exchange_list: |
| | 2109 | groups = '' |
| | 2110 | is_in_roster = True |
| | 2111 | contact = gajim.contacts.get_contact_with_highest_priority(\ |
| | 2112 | self.account, jid) |
| | 2113 | if not contact: |
| | 2114 | is_in_roster = False |
| | 2115 | name = self.exchange_list[jid][0] |
| | 2116 | num_list = len(self.exchange_list[jid][1]) |
| | 2117 | current = 0 |
| | 2118 | for group in self.exchange_list[jid][1]: |
| | 2119 | current += 1 |
| | 2120 | if contact and not group in contact.groups: |
| | 2121 | is_in_roster = False |
| | 2122 | if current == num_list: |
| | 2123 | groups = groups + group |
| | 2124 | else: |
| | 2125 | groups = groups + group + ', ' |
| | 2126 | if not is_in_roster: |
| | 2127 | iter = model.append() |
| | 2128 | model.set(iter, 0, True, 1, jid, 2, name, 3, groups) |
| | 2129 | elif action == 'modify': |
| | 2130 | for jid in self.exchange_list: |
| | 2131 | groups = '' |
| | 2132 | is_in_roster = True |
| | 2133 | is_right = True |
| | 2134 | contact = gajim.contacts.get_contact_with_highest_priority(\ |
| | 2135 | self.account, jid) |
| | 2136 | name = self.exchange_list[jid][0] |
| | 2137 | if not contact: |
| | 2138 | is_in_roster = False |
| | 2139 | is_right = False |
| | 2140 | else: |
| | 2141 | if name != contact.name: |
| | 2142 | is_right = False |
| | 2143 | num_list = len(self.exchange_list[jid][1]) |
| | 2144 | current = 0 |
| | 2145 | for group in self.exchange_list[jid][1]: |
| | 2146 | current += 1 |
| | 2147 | if contact and not group in contact.groups: |
| | 2148 | is_right = False |
| | 2149 | if current == num_list: |
| | 2150 | groups = groups + group |
| | 2151 | else: |
| | 2152 | groups = groups + group + ', ' |
| | 2153 | if not is_right and is_in_roster: |
| | 2154 | iter = model.append() |
| | 2155 | model.set(iter, 0, True, 1, jid, 2, name, 3, groups) |
| | 2156 | elif action == 'delete': |
| | 2157 | for jid in self.exchange_list: |
| | 2158 | groups = '' |
| | 2159 | is_in_roster = True |
| | 2160 | contact = gajim.contacts.get_contact_with_highest_priority(\ |
| | 2161 | self.account, jid) |
| | 2162 | name = self.exchange_list[jid][0] |
| | 2163 | if not contact: |
| | 2164 | is_in_roster = False |
| | 2165 | num_list = len(self.exchange_list[jid][1]) |
| | 2166 | current = 0 |
| | 2167 | for group in self.exchange_list[jid][1]: |
| | 2168 | current += 1 |
| | 2169 | if current == num_list: |
| | 2170 | groups = groups + group |
| | 2171 | else: |
| | 2172 | groups = groups + group + ', ' |
| | 2173 | if not is_right and is_in_roster: |
| | 2174 | iter = model.append() |
| | 2175 | model.set(iter, 0, True, 1, jid, 2, name, 3, groups) |
| | 2176 | |
| | 2177 | self.window.show_all() |
| | 2178 | |
| | 2179 | self.xml.signal_autoconnect(self) |
| | 2180 | |
| | 2181 | def toggled_callback(self, cell, path): |
| | 2182 | model = self.items_list_treeview.get_model() |
| | 2183 | iter = model.get_iter(path) |
| | 2184 | model[iter][0] = not cell.get_active() |
| | 2185 | |
| | 2186 | def on_accept_button_clicked(self, widget): |
| | 2187 | model = self.items_list_treeview.get_model() |
| | 2188 | iter = model.get_iter_root() |
| | 2189 | if self.action == 'add': |
| | 2190 | a = 0 |
| | 2191 | while iter: |
| | 2192 | if model[iter][0]: |
| | 2193 | a+=1 |
| | 2194 | # it is selected |
| | 2195 | #remote_jid = model[iter][1].decode('utf-8') |
| | 2196 | message = _('%s suggested me to add you in my roster.' % self.jid_from) |
| | 2197 | # keep same groups and same nickname |
| | 2198 | groups = model[iter][3].split(', ') |
| | 2199 | if groups == ['']: |
| | 2200 | groups = [] |
| | 2201 | gajim.interface.roster.req_sub(self, model[iter][1], message, |
| | 2202 | self.account, groups = groups, |
| | 2203 | nickname = model[iter][2], auto_auth = True) |
| | 2204 | iter = model.iter_next(iter) |
| | 2205 | InformationDialog('Added %s contacts' % str(a)) |
| | 2206 | elif self.action == 'modify': |
| | 2207 | a = 0 |
| | 2208 | while iter: |
| | 2209 | if model[iter][0]: |
| | 2210 | a+=1 |
| | 2211 | # it is selected |
| | 2212 | jid = model[iter][1].decode('utf-8') |
| | 2213 | # message = _('%s suggested me to add you in my roster.' % self.jid_from) |
| | 2214 | # keep same groups and same nickname |
| | 2215 | groups = model[iter][3].split(', ') |
| | 2216 | if groups == ['']: |
| | 2217 | groups = [] |
| | 2218 | for u in gajim.contacts.get_contact(self.account, jid): |
| | 2219 | u.name = model[iter][2] |
| | 2220 | gajim.connections[self.account].update_contact(jid, model[iter][2], groups) |
| | 2221 | self.draw_contact(jid, account) |
| | 2222 | # Update opened chat |
| | 2223 | ctrl = gajim.interface.msg_win_mgr.get_control(jid, self.account) |
| | 2224 | if ctrl: |
| | 2225 | ctrl.update_ui() |
| | 2226 | win = gajim.interface.msg_win_mgr.get_window(jid, self.account) |
| | 2227 | win.redraw_tab(ctrl) |
| | 2228 | win.show_title() |
| | 2229 | iter = model.iter_next(iter) |
| | 2230 | elif self.action == 'delete': |
| | 2231 | a = 0 |
| | 2232 | while iter: |
| | 2233 | if model[iter][0]: |
| | 2234 | a+=1 |
| | 2235 | # it is selected |
| | 2236 | jid = model[iter][1].decode('utf-8') |
| | 2237 | gajim.connections[self.account].unsubscribe(jid) |
| | 2238 | for c in gajim.contacts.get_contact(self.account, jid): |
| | 2239 | self.remove_contact(c, self.account) |
| | 2240 | gajim.contacts.remove_jid(self.account, jid) |
| | 2241 | iter = model.iter_next(iter) |
| | 2242 | InformationDialog('Added %s contacts' % str(a)) |
| | 2243 | self.window.destroy() |
| | 2244 | |