File: prefs.c

package info (click to toggle)
pidgin-encryption 3.1-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,916 kB
  • ctags: 961
  • sloc: sh: 10,460; ansic: 8,821; makefile: 376; yacc: 318
file content (148 lines) | stat: -rw-r--r-- 5,361 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*  Pidgin-Encryption Legacy Preferences file interface                   */
/*             Copyright (C) 2001-2007 William Tompkins                   */

/* This plugin is free software, distributed under the GNU General Public */
/* License.                                                               */
/* Please see the file "COPYING" distributed with this source code        */
/* for more details                                                       */
/*                                                                        */
/*                                                                        */
/*    This software is distributed in the hope that it will be useful,    */
/*   but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    */
/*   General Public License for more details.                             */

/*   To compile and use:                                                  */
/*     See INSTALL file.                                                  */

#include "internal.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#if GLIB_CHECK_VERSION(2,6,0)
#	include <glib/gstdio.h>
#else
#	define g_freopen freopen
#	define g_fopen fopen
#	define g_rmdir rmdir
#	define g_remove remove
#	define g_unlink unlink
#	define g_lstat lstat
#	define g_stat stat
#	define g_mkdir mkdir
#	define g_rename rename
#	define g_open open
#endif

#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gtk/gtkplug.h>

#include "prefs.h"
#include "util.h"
#include "prefs.h"
#include "config_ui.h"
#include "keys.h"

#ifdef _WIN32
#include "win32dep.h"
#endif

void PE_prefs_changed_cb(const char* name, PurplePrefType type, gconstpointer val, gpointer data) {
   const char * displayedpath =  purple_prefs_get_string("/plugins/gtk/encrypt/key_path_displayed");
   const char * basepath = purple_prefs_get_string("/plugins/gtk/encrypt/key_path");

   const char * previous_displayedpath = basepath;

   if (basepath && basepath[0] == 0) {
      /* if the basepath is blank, then the previous displayed path was actually the users's Purple dir */
      previous_displayedpath = purple_user_dir();
   }

   /* did the displayedpath get changed? */
   if (basepath && displayedpath && strcmp(displayedpath, previous_displayedpath) != 0) {
      /* yes, so set the underlying basepath pref appropriately */
      if (strcmp(displayedpath, purple_user_dir()) == 0) {
         purple_prefs_set_string("/plugins/gtk/encrypt/key_path", "");
      } else {
         purple_prefs_set_string("/plugins/gtk/encrypt/key_path", displayedpath);
      }
   }

   if (PE_check_base_key_path()) {
      PE_key_rings_init();
      PE_config_update();
   } else {
      purple_debug(PURPLE_DEBUG_INFO, "pidgin-encryption", "pref_changed_cb: %s\n", (char*)val);
      if (g_path_is_absolute(val)) {
         PE_config_show_invalid_keypath();
      } else {
         purple_prefs_set_string("/plugins/gtk/encrypt/key_path", "");
         purple_prefs_set_string("/plugins/gtk/encrypt/key_path_displayed", purple_user_dir());
         PE_config_show_nonabsolute_keypath();
      }
   }
}


/* Old self-kept preferences: only here to convert to new Purple-kept prefs */
static gboolean Prefs_accept_key_unknown = FALSE;
static gboolean Prefs_accept_key_conflict = FALSE;
/*static gboolean Prefs_encrypt_response = TRUE; */
static gboolean Prefs_broadcast_notify = FALSE;
static gboolean Prefs_encrypt_if_notified = TRUE;

const static char key_file[] = "encrypt.prefs";

static gboolean parse_key_val(char* val, gboolean def) {
   if (strcmp(val, "TRUE") == 0) {
      return TRUE;
   }
   if (strcmp(val, "FALSE") == 0) {
      return FALSE;
   }
   return def;
}

void PE_convert_legacy_prefs() {
   char key[51], value[51];
   
   char* filename = g_build_filename(purple_user_dir(), key_file, NULL);

   FILE* fp = g_fopen(filename, "r");

   purple_debug(PURPLE_DEBUG_INFO, "pidgin-encryption", "Checking for old prefs file (%s)...\n", filename);

   if (fp == NULL) {
      g_free(filename);
      return;
   }   
   purple_debug(PURPLE_DEBUG_INFO, "pidgin-encryption", "Converting...\n");

   while (fscanf(fp, "%50s%50s", key, value) != EOF) {
      if (strcmp(key, "AcceptUnknown") == 0) {
         purple_prefs_set_bool("/plugins/gtk/encrypt/accept_unknown_key",
                             parse_key_val(value, Prefs_accept_key_unknown));
      } else if (strcmp(key, "AcceptDuplicate") == 0) {
         purple_prefs_set_bool("/plugins/gtk/encrypt/accept_conflicting_key",
                             parse_key_val(value, Prefs_accept_key_conflict));
      } else if (strcmp(key, "BroadcastNotify") == 0) {
         purple_prefs_set_bool("/plugins/gtk/encrypt/broadcast_notify",
                             parse_key_val(value, Prefs_broadcast_notify));
      } else if (strcmp(key, "EncryptIfNotified") == 0) {
         purple_prefs_set_bool("/plugins/gtk/encrypt/encrypt_if_notified",
                             parse_key_val(value, Prefs_encrypt_if_notified));
      } else {
         purple_debug(PURPLE_DEBUG_ERROR, "pidgin-encryption", "Bad Preference Key %s\n", value);
      }
   }
   fclose(fp);

   purple_debug(PURPLE_DEBUG_INFO, "pidgin-encryption", "Deleting old prefs\n");
   unlink(filename);

   g_free(filename);
}