File: state.c

package info (click to toggle)
gaim-encryption 3.0~beta7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,984 kB
  • ctags: 849
  • sloc: ansic: 8,667; sh: 8,547; makefile: 378; yacc: 318
file content (195 lines) | stat: -rw-r--r-- 4,972 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <string.h>
#include <ctype.h>

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

#include <gtkplugin.h>

#include <debug.h>
#include <util.h>
#include <conversation.h>

#include "ge_blist.h"
#include "state_ui.h"
#include "state.h"

GHashTable *encryption_state_table; /* name -> EncryptionState */

/* Helper function: */
static void reset_state_struct(const GaimAccount* account,
                               const gchar* name,
                               EncryptionState* state);

void GE_state_init() {
   encryption_state_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}

void GE_state_delete() {
   g_hash_table_destroy(encryption_state_table);
}

EncryptionState* GE_get_state(GaimConversation* conv) {
   if (conv == NULL) return NULL;

   EncryptionState *state = gaim_conversation_get_data(conv, "GE_state");

   if (state == NULL) {
      state = g_malloc(sizeof(EncryptionState));
      gaim_conversation_set_data(conv, "GE_state", state);

      // should probably change this to use some prefs info rather than our buddy list stuff
      state->outgoing_encrypted =
         GE_get_buddy_default_autoencrypt(gaim_conversation_get_account(conv),
                                          gaim_conversation_get_name(conv));
      
      if (conv == NULL) {
         return NULL;
      }

      state->has_been_notified =
         GE_get_default_notified(gaim_conversation_get_account(conv),
                                 gaim_conversation_get_name(conv));
      
      state->incoming_encrypted = FALSE;
      state->is_capable = FALSE;      
   }
   return state;
}

void GE_reset_state(GaimConversation* conv) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   reset_state_struct(gaim_conversation_get_account(conv), gaim_conversation_get_name(conv), state);
}

void GE_free_state(GaimConversation* conv) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   if (state) {
      g_free(state);
   }
}

static void reset_state_struct(const GaimAccount* account, const gchar* name,
                               EncryptionState* state) {

   state->outgoing_encrypted = GE_get_buddy_default_autoencrypt(account, name);
   state->has_been_notified = GE_get_default_notified(account, name);

   state->incoming_encrypted = FALSE;
   state->is_capable = FALSE;
}

void GE_set_tx_encryption(GaimConversation* conv, gboolean do_encrypt) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   if (state->outgoing_encrypted != do_encrypt) {
      state->outgoing_encrypted = do_encrypt;
      GE_sync_state(conv);
   }
}

void GE_set_capable(GaimConversation* conv, gboolean cap) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   if (state->is_capable != cap) {
      state->is_capable = cap;
      if (state->outgoing_encrypted == FALSE) {
         GE_sync_state(conv);
      }
   }
}

void GE_set_rx_encryption(GaimConversation *conv, gboolean incoming_encrypted) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   if (state->incoming_encrypted != incoming_encrypted) {
      state->incoming_encrypted = incoming_encrypted;
      GE_set_rx_encryption_icon(conv, incoming_encrypted);
   }
}

gboolean GE_get_tx_encryption(GaimConversation *conv) {
   EncryptionState *state = GE_get_state(conv);

   if (state == NULL) return FALSE;

   return state->outgoing_encrypted;
}

void GE_sync_state(GaimConversation *conv) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);
   GE_set_rx_encryption_icon(conv, state->incoming_encrypted);
   GE_set_tx_encryption_icon(conv, state->outgoing_encrypted, state->is_capable);
}

gboolean GE_has_been_notified(GaimConversation *conv) {
   EncryptionState *state = GE_get_state(conv);

   if (state == NULL) return TRUE;

   return state->has_been_notified;
}

void GE_set_notified(GaimConversation *conv, gboolean new_state) {
   EncryptionState *state;

   if (conv == NULL) return;

   state = GE_get_state(conv);

   state->has_been_notified = new_state;
}


gboolean GE_get_default_notified(const GaimAccount *account, const gchar* name) {
   /* Most protocols no longer have a notify message, since they don't do HTML */
   
   /* The only special case here is Oscar/TOC: If the other user's name is all */
   /* digits, then they're ICQ, so we pretend that we already notified them    */

   const char* protocol_id = gaim_account_get_protocol_id(account);
   
   if (strcmp(protocol_id, "prpl-toc") == 0 || strcmp(protocol_id, "prpl-oscar") == 0) {
      
      while(*name != 0) {
         if (!isdigit(*name++)) {
            /* not ICQ */
            return FALSE;
         }
      }
      /* Hrm.  must be ICQ */
      return TRUE;
   }

   /* default to notifying next time, if protocol allows it */

   return FALSE;
}