1 /**
2  * @file auth_dialog.c  authentication dialog
3  *
4  * Copyright (C) 2007-2016  Lars Windolf <lars.windolf@gmx.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include "ui/auth_dialog.h"
22 
23 #include <libxml/uri.h>
24 
25 #include "common.h"
26 #include "debug.h"
27 #include "ui/liferea_dialog.h"
28 
29 #define AUTH_DIALOG_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), AUTH_DIALOG_TYPE, AuthDialogPrivate))
30 
31 struct AuthDialogPrivate {
32 
33 	subscriptionPtr subscription;
34 
35 	GtkWidget *dialog;
36 	GtkWidget *username;
37 	GtkWidget *password;
38 
39 	gint flags;
40 };
41 
42 static GObjectClass *parent_class = NULL;
43 
44 G_DEFINE_TYPE (AuthDialog, auth_dialog, G_TYPE_OBJECT);
45 
46 static void
auth_dialog_finalize(GObject * object)47 auth_dialog_finalize (GObject *object)
48 {
49 	AuthDialog *ad = AUTH_DIALOG (object);
50 
51 	if (ad->priv->subscription != NULL)
52 		ad->priv->subscription->activeAuth = FALSE;
53 
54 	gtk_widget_destroy (ad->priv->dialog);
55 
56 	G_OBJECT_CLASS (parent_class)->finalize (object);
57 }
58 
59 static void
auth_dialog_class_init(AuthDialogClass * klass)60 auth_dialog_class_init (AuthDialogClass *klass)
61 {
62 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
63 
64 	parent_class = g_type_class_peek_parent (klass);
65 
66 	object_class->finalize = auth_dialog_finalize;
67 
68 	g_type_class_add_private (object_class, sizeof(AuthDialogPrivate));
69 }
70 
71 static void
on_authdialog_response(GtkDialog * dialog,gint response_id,gpointer user_data)72 on_authdialog_response (GtkDialog *dialog,
73                         gint response_id,
74 			gpointer user_data)
75 {
76 	AuthDialog *ad = AUTH_DIALOG (user_data);
77 
78 	if (response_id == GTK_RESPONSE_OK) {
79 		subscription_set_auth_info (ad->priv->subscription,
80 		                            gtk_entry_get_text (GTK_ENTRY (ad->priv->username)),
81 		                            gtk_entry_get_text (GTK_ENTRY (ad->priv->password)));
82 		subscription_update (ad->priv->subscription, ad->priv->flags);
83 	}
84 
85 	g_object_unref (ad);
86 }
87 
88 static void
auth_dialog_load(AuthDialog * ad,subscriptionPtr subscription,gint flags)89 auth_dialog_load (AuthDialog *ad,
90                   subscriptionPtr subscription,
91                   gint flags)
92 {
93 	gchar			*promptStr;
94 	gchar			*source = NULL;
95 	xmlURIPtr		uri;
96 
97 	subscription->activeAuth = TRUE;
98 
99 	ad->priv->subscription = subscription;
100 	ad->priv->flags = flags;
101 
102 	uri = xmlParseURI (subscription_get_source (ad->priv->subscription));
103 
104 	if (uri) {
105 		if (uri->user) {
106 			gchar *user = uri->user;
107 			gchar *pass = strstr (user, ":");
108 			if(pass) {
109 				pass[0] = '\0';
110 				pass++;
111 				gtk_entry_set_text (GTK_ENTRY (ad->priv->password), pass);
112 			}
113 			gtk_entry_set_text (GTK_ENTRY (ad->priv->username), user);
114 			xmlFree (uri->user);
115 			uri->user = NULL;
116 		}
117 		xmlFree (uri->user);
118 		uri->user = NULL;
119 		source = (gchar *) xmlSaveUri (uri);
120 		xmlFreeURI (uri);
121 	}
122 
123 	promptStr = g_strdup_printf ( _("Enter the username and password for \"%s\" (%s):"),
124 	                             node_get_title (ad->priv->subscription->node),
125 	                             source?source:_("Unknown source"));
126 	gtk_label_set_text (GTK_LABEL (liferea_dialog_lookup (ad->priv->dialog, "prompt")), promptStr);
127 	g_free (promptStr);
128 	if (source)
129 		xmlFree (source);
130 }
131 
132 static void
auth_dialog_init(AuthDialog * ad)133 auth_dialog_init (AuthDialog *ad)
134 {
135 	ad->priv = AUTH_DIALOG_GET_PRIVATE (ad);
136 
137 	ad->priv->dialog = liferea_dialog_new ("auth");
138 	ad->priv->username = liferea_dialog_lookup (ad->priv->dialog, "usernameEntry");
139 	ad->priv->password = liferea_dialog_lookup (ad->priv->dialog, "passwordEntry");
140 
141 	g_signal_connect (G_OBJECT (ad->priv->dialog), "response", G_CALLBACK (on_authdialog_response), ad);
142 
143 	gtk_widget_show_all (ad->priv->dialog);
144 }
145 
146 
147 AuthDialog *
auth_dialog_new(subscriptionPtr subscription,gint flags)148 auth_dialog_new (subscriptionPtr subscription, gint flags)
149 {
150 	AuthDialog *ad;
151 
152 	if (subscription->activeAuth) {
153 		debug0 (DEBUG_UPDATE, "Missing/wrong authentication. Skipping, as a dialog is already active.");
154 		return NULL;
155 	}
156 
157 	ad = AUTH_DIALOG (g_object_new (AUTH_DIALOG_TYPE, NULL));
158 	auth_dialog_load(ad, subscription, flags);
159 	return ad;
160 }
161