From 226e5372e4f6a9029a763bcdb851fa3e128b8ee1 Mon Sep 17 00:00:00 2001
From: Dimitur Kirov <dkirov@gmail.com>
Date: Fri, 19 May 2006 21:13:45 +0000
Subject: [PATCH] new arguments for gajim-remote add_contact (<jid> [account]).
 If account is not specified, show a combobox with active accounts

---
 data/glade/add_new_contact_window.glade | 254 ++++++++++++++----------
 src/dialogs.py                          |  60 ++++--
 src/gajim-remote.py                     |   3 +-
 src/remote_control.py                   |  19 +-
 4 files changed, 211 insertions(+), 125 deletions(-)

diff --git a/data/glade/add_new_contact_window.glade b/data/glade/add_new_contact_window.glade
index 791b3c5b7e..77d62ceb81 100644
--- a/data/glade/add_new_contact_window.glade
+++ b/data/glade/add_new_contact_window.glade
@@ -54,17 +54,116 @@
 	<widget class="GtkTable" id="table21">
 	  <property name="border_width">6</property>
 	  <property name="visible">True</property>
-	  <property name="n_rows">6</property>
+	  <property name="n_rows">7</property>
 	  <property name="n_columns">2</property>
 	  <property name="homogeneous">False</property>
 	  <property name="row_spacing">6</property>
 	  <property name="column_spacing">6</property>
 
 	  <child>
-	    <widget class="GtkLabel" id="label185">
+	    <widget class="GtkCheckButton" id="auto_authorize_checkbutton">
+	      <property name="visible">True</property>
+	      <property name="can_focus">True</property>
+	      <property name="label" translatable="yes">Au_tomatically authorize contact</property>
+	      <property name="use_underline">True</property>
+	      <property name="relief">GTK_RELIEF_NORMAL</property>
+	      <property name="focus_on_click">True</property>
+	      <property name="active">True</property>
+	      <property name="inconsistent">False</property>
+	      <property name="draw_indicator">True</property>
+	    </widget>
+	    <packing>
+	      <property name="left_attach">1</property>
+	      <property name="right_attach">2</property>
+	      <property name="top_attach">6</property>
+	      <property name="bottom_attach">7</property>
+	      <property name="x_options">fill</property>
+	      <property name="y_options"></property>
+	    </packing>
+	  </child>
+
+	  <child>
+	    <widget class="GtkComboBoxEntry" id="group_comboboxentry">
+	      <property name="visible">True</property>
+	      <property name="items" translatable="yes"></property>
+	      <property name="add_tearoffs">False</property>
+	      <property name="has_frame">True</property>
+	      <property name="focus_on_click">True</property>
+	    </widget>
+	    <packing>
+	      <property name="left_attach">1</property>
+	      <property name="right_attach">2</property>
+	      <property name="top_attach">5</property>
+	      <property name="bottom_attach">6</property>
+	      <property name="x_options">fill</property>
+	    </packing>
+	  </child>
+
+	  <child>
+	    <widget class="GtkEntry" id="nickname_entry">
+	      <property name="visible">True</property>
+	      <property name="can_focus">True</property>
+	      <property name="editable">True</property>
+	      <property name="visibility">True</property>
+	      <property name="max_length">0</property>
+	      <property name="text" translatable="yes"></property>
+	      <property name="has_frame">True</property>
+	      <property name="invisible_char">*</property>
+	      <property name="activates_default">True</property>
+	    </widget>
+	    <packing>
+	      <property name="left_attach">1</property>
+	      <property name="right_attach">2</property>
+	      <property name="top_attach">4</property>
+	      <property name="bottom_attach">5</property>
+	      <property name="y_options"></property>
+	    </packing>
+	  </child>
+
+	  <child>
+	    <widget class="GtkEntry" id="jid_entry">
 	      <property name="visible">True</property>
-	      <property name="label" translatable="yes">User ID:</property>
-	      <property name="use_underline">False</property>
+	      <property name="sensitive">False</property>
+	      <property name="editable">False</property>
+	      <property name="visibility">True</property>
+	      <property name="max_length">0</property>
+	      <property name="text" translatable="yes"></property>
+	      <property name="has_frame">True</property>
+	      <property name="invisible_char">*</property>
+	      <property name="activates_default">False</property>
+	    </widget>
+	    <packing>
+	      <property name="left_attach">1</property>
+	      <property name="right_attach">2</property>
+	      <property name="top_attach">3</property>
+	      <property name="bottom_attach">4</property>
+	      <property name="y_options"></property>
+	    </packing>
+	  </child>
+
+	  <child>
+	    <widget class="GtkComboBox" id="protocol_combobox">
+	      <property name="visible">True</property>
+	      <property name="items" translatable="yes"></property>
+	      <property name="add_tearoffs">False</property>
+	      <property name="focus_on_click">True</property>
+	      <signal name="changed" handler="on_protocol_combobox_changed" last_modification_time="Wed, 23 Mar 2005 13:13:12 GMT"/>
+	    </widget>
+	    <packing>
+	      <property name="left_attach">1</property>
+	      <property name="right_attach">2</property>
+	      <property name="top_attach">2</property>
+	      <property name="bottom_attach">3</property>
+	      <property name="x_options">fill</property>
+	      <property name="y_options">fill</property>
+	    </packing>
+	  </child>
+
+	  <child>
+	    <widget class="GtkLabel" id="label223">
+	      <property name="visible">True</property>
+	      <property name="label" translatable="yes">_Group:</property>
+	      <property name="use_underline">True</property>
 	      <property name="use_markup">False</property>
 	      <property name="justify">GTK_JUSTIFY_LEFT</property>
 	      <property name="wrap">False</property>
@@ -81,18 +180,18 @@
 	    <packing>
 	      <property name="left_attach">0</property>
 	      <property name="right_attach">1</property>
-	      <property name="top_attach">0</property>
-	      <property name="bottom_attach">1</property>
+	      <property name="top_attach">5</property>
+	      <property name="bottom_attach">6</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkLabel" id="protocol_label">
+	    <widget class="GtkLabel" id="label188">
 	      <property name="visible">True</property>
-	      <property name="label" translatable="yes">Protocol:</property>
-	      <property name="use_underline">False</property>
+	      <property name="label" translatable="yes">_Nickname:</property>
+	      <property name="use_underline">True</property>
 	      <property name="use_markup">False</property>
 	      <property name="justify">GTK_JUSTIFY_LEFT</property>
 	      <property name="wrap">False</property>
@@ -101,6 +200,7 @@
 	      <property name="yalign">0.5</property>
 	      <property name="xpad">0</property>
 	      <property name="ypad">0</property>
+	      <property name="mnemonic_widget">nickname_entry</property>
 	      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
 	      <property name="width_chars">-1</property>
 	      <property name="single_line_mode">False</property>
@@ -109,8 +209,8 @@
 	    <packing>
 	      <property name="left_attach">0</property>
 	      <property name="right_attach">1</property>
-	      <property name="top_attach">1</property>
-	      <property name="bottom_attach">2</property>
+	      <property name="top_attach">4</property>
+	      <property name="bottom_attach">5</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options"></property>
 	    </packing>
@@ -119,8 +219,8 @@
 	  <child>
 	    <widget class="GtkLabel" id="label187">
 	      <property name="visible">True</property>
-	      <property name="label" translatable="yes">Jabber ID:</property>
-	      <property name="use_underline">False</property>
+	      <property name="label" translatable="yes">_Jabber ID:</property>
+	      <property name="use_underline">True</property>
 	      <property name="use_markup">False</property>
 	      <property name="justify">GTK_JUSTIFY_LEFT</property>
 	      <property name="wrap">False</property>
@@ -137,18 +237,18 @@
 	    <packing>
 	      <property name="left_attach">0</property>
 	      <property name="right_attach">1</property>
-	      <property name="top_attach">2</property>
-	      <property name="bottom_attach">3</property>
+	      <property name="top_attach">3</property>
+	      <property name="bottom_attach">4</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkLabel" id="label188">
+	    <widget class="GtkLabel" id="protocol_label">
 	      <property name="visible">True</property>
-	      <property name="label" translatable="yes">Nickname:</property>
-	      <property name="use_underline">False</property>
+	      <property name="label" translatable="yes">_Protocol:</property>
+	      <property name="use_underline">True</property>
 	      <property name="use_markup">False</property>
 	      <property name="justify">GTK_JUSTIFY_LEFT</property>
 	      <property name="wrap">False</property>
@@ -165,55 +265,44 @@
 	    <packing>
 	      <property name="left_attach">0</property>
 	      <property name="right_attach">1</property>
-	      <property name="top_attach">3</property>
-	      <property name="bottom_attach">4</property>
+	      <property name="top_attach">2</property>
+	      <property name="bottom_attach">3</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkEntry" id="uid_entry">
-	      <property name="visible">True</property>
-	      <property name="can_focus">True</property>
-	      <property name="editable">True</property>
-	      <property name="visibility">True</property>
-	      <property name="max_length">0</property>
-	      <property name="text" translatable="yes"></property>
-	      <property name="has_frame">True</property>
-	      <property name="invisible_char">*</property>
-	      <property name="activates_default">True</property>
-	      <signal name="changed" handler="on_uid_entry_changed" last_modification_time="Mon, 28 Feb 2005 23:05:24 GMT"/>
-	    </widget>
-	    <packing>
-	      <property name="left_attach">1</property>
-	      <property name="right_attach">2</property>
-	      <property name="top_attach">0</property>
-	      <property name="bottom_attach">1</property>
-	      <property name="y_options"></property>
-	    </packing>
-	  </child>
-
-	  <child>
-	    <widget class="GtkComboBox" id="protocol_combobox">
+	    <widget class="GtkLabel" id="label185">
 	      <property name="visible">True</property>
-	      <property name="items" translatable="yes"></property>
-	      <property name="add_tearoffs">False</property>
-	      <property name="focus_on_click">True</property>
-	      <signal name="changed" handler="on_protocol_combobox_changed" last_modification_time="Wed, 23 Mar 2005 13:13:12 GMT"/>
+	      <property name="label" translatable="yes">_User ID:</property>
+	      <property name="use_underline">True</property>
+	      <property name="use_markup">False</property>
+	      <property name="justify">GTK_JUSTIFY_LEFT</property>
+	      <property name="wrap">False</property>
+	      <property name="selectable">False</property>
+	      <property name="xalign">0</property>
+	      <property name="yalign">0.5</property>
+	      <property name="xpad">0</property>
+	      <property name="ypad">0</property>
+	      <property name="mnemonic_widget">uid_entry</property>
+	      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+	      <property name="width_chars">-1</property>
+	      <property name="single_line_mode">False</property>
+	      <property name="angle">0</property>
 	    </widget>
 	    <packing>
-	      <property name="left_attach">1</property>
-	      <property name="right_attach">2</property>
+	      <property name="left_attach">0</property>
+	      <property name="right_attach">1</property>
 	      <property name="top_attach">1</property>
 	      <property name="bottom_attach">2</property>
 	      <property name="x_options">fill</property>
-	      <property name="y_options">fill</property>
+	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkEntry" id="nickname_entry">
+	    <widget class="GtkEntry" id="uid_entry">
 	      <property name="visible">True</property>
 	      <property name="can_focus">True</property>
 	      <property name="editable">True</property>
@@ -223,64 +312,22 @@
 	      <property name="has_frame">True</property>
 	      <property name="invisible_char">*</property>
 	      <property name="activates_default">True</property>
+	      <signal name="changed" handler="on_uid_entry_changed" last_modification_time="Mon, 28 Feb 2005 23:05:24 GMT"/>
 	    </widget>
 	    <packing>
 	      <property name="left_attach">1</property>
 	      <property name="right_attach">2</property>
-	      <property name="top_attach">3</property>
-	      <property name="bottom_attach">4</property>
-	      <property name="y_options"></property>
-	    </packing>
-	  </child>
-
-	  <child>
-	    <widget class="GtkEntry" id="jid_entry">
-	      <property name="visible">True</property>
-	      <property name="sensitive">False</property>
-	      <property name="editable">False</property>
-	      <property name="visibility">True</property>
-	      <property name="max_length">0</property>
-	      <property name="text" translatable="yes"></property>
-	      <property name="has_frame">True</property>
-	      <property name="invisible_char">*</property>
-	      <property name="activates_default">False</property>
-	    </widget>
-	    <packing>
-	      <property name="left_attach">1</property>
-	      <property name="right_attach">2</property>
-	      <property name="top_attach">2</property>
-	      <property name="bottom_attach">3</property>
+	      <property name="top_attach">1</property>
+	      <property name="bottom_attach">2</property>
 	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkCheckButton" id="auto_authorize_checkbutton">
+	    <widget class="GtkLabel" id="account_label">
 	      <property name="visible">True</property>
-	      <property name="can_focus">True</property>
-	      <property name="label" translatable="yes">Automatically authorize contact</property>
+	      <property name="label" translatable="yes">_Account:</property>
 	      <property name="use_underline">True</property>
-	      <property name="relief">GTK_RELIEF_NORMAL</property>
-	      <property name="focus_on_click">True</property>
-	      <property name="active">True</property>
-	      <property name="inconsistent">False</property>
-	      <property name="draw_indicator">True</property>
-	    </widget>
-	    <packing>
-	      <property name="left_attach">1</property>
-	      <property name="right_attach">2</property>
-	      <property name="top_attach">5</property>
-	      <property name="bottom_attach">6</property>
-	      <property name="x_options">fill</property>
-	      <property name="y_options"></property>
-	    </packing>
-	  </child>
-
-	  <child>
-	    <widget class="GtkLabel" id="label223">
-	      <property name="visible">True</property>
-	      <property name="label" translatable="yes">Group:</property>
-	      <property name="use_underline">False</property>
 	      <property name="use_markup">False</property>
 	      <property name="justify">GTK_JUSTIFY_LEFT</property>
 	      <property name="wrap">False</property>
@@ -297,26 +344,25 @@
 	    <packing>
 	      <property name="left_attach">0</property>
 	      <property name="right_attach">1</property>
-	      <property name="top_attach">4</property>
-	      <property name="bottom_attach">5</property>
+	      <property name="top_attach">0</property>
+	      <property name="bottom_attach">1</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options"></property>
 	    </packing>
 	  </child>
 
 	  <child>
-	    <widget class="GtkComboBoxEntry" id="group_comboboxentry">
+	    <widget class="GtkComboBox" id="account_combobox">
 	      <property name="visible">True</property>
 	      <property name="items" translatable="yes"></property>
 	      <property name="add_tearoffs">False</property>
-	      <property name="has_frame">True</property>
 	      <property name="focus_on_click">True</property>
 	    </widget>
 	    <packing>
 	      <property name="left_attach">1</property>
 	      <property name="right_attach">2</property>
-	      <property name="top_attach">4</property>
-	      <property name="bottom_attach">5</property>
+	      <property name="top_attach">0</property>
+	      <property name="bottom_attach">1</property>
 	      <property name="x_options">fill</property>
 	      <property name="y_options">fill</property>
 	    </packing>
diff --git a/src/dialogs.py b/src/dialogs.py
index da4b4dc4b3..58ac9b086b 100644
--- a/src/dialogs.py
+++ b/src/dialogs.py
@@ -362,15 +362,29 @@ class ChangeStatusMessageDialog:
 
 class AddNewContactWindow:
 	'''Class for AddNewContactWindow'''
-	def __init__(self, account, jid = None):
+	def __init__(self, account = None, jid = None):
 		self.account = account
+		if account == None:
+			# fill accounts with active accounts
+			accounts = []
+			for account in gajim.connections.keys():
+				if gajim.connections[account].connected > 1:
+					accounts.append(account)
+			if not accounts:
+				return
+			if len(accounts) == 1:
+				self.account = account
+		else:
+			accounts = [self.account]
 		self.xml = gtkgui_helpers.get_glade('add_new_contact_window.glade')
+		self.account_combobox = self.xml.get_widget('account_combobox')
+		self.account_label = self.xml.get_widget('account_label')
 		self.window = self.xml.get_widget('add_new_contact_window')
 		self.uid_entry = self.xml.get_widget('uid_entry')
 		self.protocol_combobox = self.xml.get_widget('protocol_combobox')
 		self.jid_entry = self.xml.get_widget('jid_entry')
 		self.nickname_entry = self.xml.get_widget('nickname_entry')
-		if len(gajim.connections) >= 2:
+		if account and len(gajim.connections) >= 2:
 			prompt_text =\
 _('Please fill in the data of the contact you want to add in account %s') %account
 		else:
@@ -381,11 +395,12 @@ _('Please fill in the data of the contact you want to add in account %s') %accou
 		liststore.append(['Jabber', ''])
 		self.agents = ['Jabber']
 		jid_agents = []
-		for j in gajim.contacts.get_jid_list(account):
-			contact = gajim.contacts.get_first_contact_from_jid(account, j)
-			if _('Transports') in contact.groups and contact.show != 'offline' and\
-					contact.show != 'error':
-				jid_agents.append(j)
+		for acct in accounts:
+			for j in gajim.contacts.get_jid_list(acct):
+				contact = gajim.contacts.get_first_contact_from_jid(acct, j)
+				if _('Transports') in contact.groups and contact.show != 'offline' and\
+						contact.show != 'error':
+					jid_agents.append(j)
 		for a in jid_agents:
 			if a.find('aim') > -1:
 				name = 'AIM'
@@ -397,9 +412,8 @@ _('Please fill in the data of the contact you want to add in account %s') %accou
 				name = 'Yahoo!'
 			else:
 				name = a
-			iter = liststore.append([name, a])
+			liststore.append([name, a])
 			self.agents.append(name)
-		
 		self.protocol_combobox.set_model(liststore)
 		self.protocol_combobox.set_active(0)
 		self.fill_jid()
@@ -417,13 +431,15 @@ _('Please fill in the data of the contact you want to add in account %s') %accou
 				self.protocol_combobox.set_active(0)
 			self.set_nickname()
 			self.nickname_entry.grab_focus()
-
 		self.group_comboboxentry = self.xml.get_widget('group_comboboxentry')
 		liststore = gtk.ListStore(str)
 		self.group_comboboxentry.set_model(liststore)
-		for g in gajim.groups[account].keys():
-			if g not in helpers.special_groups:
-				self.group_comboboxentry.append_text(g)
+		group_names = []
+		for acct in accounts:
+			for g in gajim.groups[acct].keys():
+				if g not in helpers.special_groups and g not in group_names:
+					group_names.append(g)
+					self.group_comboboxentry.append_text(g)
 
 		if not jid_agents:
 			# There are no transports, so hide the protocol combobox and label
@@ -432,7 +448,17 @@ _('Please fill in the data of the contact you want to add in account %s') %accou
 			protocol_label = self.xml.get_widget('protocol_label')
 			protocol_label.hide()
 			protocol_label.set_no_show_all(True)
-
+		if self.account:
+			self.account_label.hide()
+			self.account_combobox.hide()
+			self.account_label.set_no_show_all(True)
+			self.account_combobox.set_no_show_all(True)
+		else:
+			liststore = gtk.ListStore(str, str)
+			for acct in accounts:
+				liststore.append([acct, acct])
+			self.account_combobox.set_model(liststore)
+			self.account_combobox.set_active(0)
 		self.xml.signal_autoconnect(self)
 		self.window.show_all()
 
@@ -465,6 +491,12 @@ _('Please fill in the data of the contact you want to add in account %s') %accou
 			ErrorDialog(pritext, _('The user ID must not contain a resource.'))
 			return
 
+		# get value of account combobox, if account was not specified
+		if not self.account:
+			model = self.account_combobox.get_model()
+			index = self.account_combobox.get_active()
+			self.account = model[index][1]
+
 		# Check if jid is already in roster
 		if jid in gajim.contacts.get_jid_list(self.account):
 			c = gajim.contacts.get_first_contact_from_jid(self.account, jid)
diff --git a/src/gajim-remote.py b/src/gajim-remote.py
index 0f3b4c2430..478990092a 100755
--- a/src/gajim-remote.py
+++ b/src/gajim-remote.py
@@ -194,7 +194,8 @@ class GajimRemote:
 			'add_contact': [
 					_('Adds contact to roster'),
 					[ 
-						(_('account'), _('Adds new contact to this account.'), True)
+						(_('jid'), _('JID of the contact'), True),
+						(_('account'), _('Adds new contact to this account'), False)
 					]
 				],
 			
diff --git a/src/remote_control.py b/src/remote_control.py
index e455db3628..52b36acb95 100644
--- a/src/remote_control.py
+++ b/src/remote_control.py
@@ -472,12 +472,19 @@ class SignalObject(DbusPrototype):
 		return True
 	
 	def add_contact(self, *args):
-		[account] = self._get_real_arguments(args, 1)
-		accounts = gajim.contacts.get_accounts()
-		if account in accounts:
-			AddNewContactWindow(account)
-			return True
-		return False
+		[jid, account] = self._get_real_arguments(args, 2)
+		if account:
+			if account in gajim.connections and \
+				gajim.connections[account].connected > 1:
+				# if given account is active, use it 
+				AddNewContactWindow(account = account, jid = jid)
+			else:
+				# wrong account
+				return False
+		else:
+			# if account is not given, show account combobox
+			AddNewContactWindow(account = None, jid = jid)
+		return True
 	
 	def remove_contact(self, *args):
 		[jid, account] = self._get_real_arguments(args, 2)
-- 
GitLab