]> www.fi.muni.cz Git - evince.git/blob - shell/ev-keyring.c
shell: Add a new sidebar page for bookmarks
[evince.git] / shell / ev-keyring.c
1 /* ev-keyring.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * 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  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "ev-keyring.h"
26
27 #ifdef WITH_KEYRING
28 #include <gnome-keyring.h>
29
30 static const GnomeKeyringPasswordSchema doc_password_schema = {
31         GNOME_KEYRING_ITEM_GENERIC_SECRET,
32         {
33                 { "type", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
34                 { "uri",  GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
35                 { NULL, 0 }
36         }
37 };
38 const GnomeKeyringPasswordSchema *EV_DOCUMENT_PASSWORD_SCHEMA = &doc_password_schema;
39 #endif /* WITH_KEYRING */
40
41 gboolean
42 ev_keyring_is_available (void)
43 {
44 #ifdef WITH_KEYRING
45         return gnome_keyring_is_available ();
46 #else
47         return FALSE;
48 #endif
49 }
50
51 gchar *
52 ev_keyring_lookup_password (const gchar *uri)
53 {
54         gchar             *retval = NULL;
55 #ifdef WITH_KEYRING
56         GnomeKeyringResult result;
57         gchar             *password = NULL;
58         
59         g_return_val_if_fail (uri != NULL, NULL);
60
61         if (!gnome_keyring_is_available ())
62                 return NULL;
63         
64         result = gnome_keyring_find_password_sync (EV_DOCUMENT_PASSWORD_SCHEMA,
65                                                    &password,
66                                                    "type", "document_password",
67                                                    "uri", uri,
68                                                    NULL);
69         if (result != GNOME_KEYRING_RESULT_OK || !password) {
70                 if (password)
71                         gnome_keyring_free_password (password);
72                 return NULL;
73         }
74
75         retval = g_strdup (password);
76         gnome_keyring_free_password (password);
77 #endif /* WITH_KEYRING */
78         return retval;
79 }
80
81 gboolean
82 ev_keyring_save_password (const gchar  *uri,
83                           const gchar  *password,
84                           GPasswordSave flags)
85 {
86 #ifdef WITH_KEYRING
87         GnomeKeyringResult result;
88         const gchar       *keyring;
89         gchar             *name;
90         gchar             *unescaped_uri;
91
92         g_return_val_if_fail (uri != NULL, FALSE);
93
94         if (!gnome_keyring_is_available ())
95                 return FALSE;
96         
97         if (flags == G_PASSWORD_SAVE_NEVER)
98                 return FALSE;
99
100         keyring = (flags == G_PASSWORD_SAVE_FOR_SESSION) ? "session" : NULL;
101         unescaped_uri = g_uri_unescape_string (uri, NULL);
102         name = g_strdup_printf (_("Password for document %s"), unescaped_uri);
103         g_free (unescaped_uri);
104         
105         result = gnome_keyring_store_password_sync (EV_DOCUMENT_PASSWORD_SCHEMA,
106                                                     keyring, name, password,
107                                                     "type", "document_password",
108                                                     "uri", uri,
109                                                     NULL);
110         g_free (name);
111
112         return (result == GNOME_KEYRING_RESULT_OK);
113 #else
114         return FALSE;
115 #endif /* WITH_KEYRING */
116 }