@@ -22,6 +22,9 @@ use crate::ui;
22
22
use std:: cell:: RefCell ;
23
23
use std:: rc:: Rc ;
24
24
25
+ //@TODO find a better place for this
26
+ const TEST_SERVER_EMAIL : & str =
"[email protected] " ;
27
+
25
28
#[ derive( Debug ) ]
26
29
pub enum ApplicationMessage {
27
30
Setup { } ,
@@ -90,6 +93,23 @@ impl ApplicationMessage {
90
93
gmail_refresh_token : response_token. refresh_token ,
91
94
}
92
95
}
96
+
97
+ pub fn save_identity_message_for_test_server ( ) -> ApplicationMessage {
98
+ ApplicationMessage :: SaveIdentity {
99
+ email_address : TEST_SERVER_EMAIL . to_string ( ) ,
100
+ full_name : "Full name" . to_string ( ) ,
101
+ account_name : "Test" . to_string ( ) ,
102
+ identity_type : models:: IdentityType :: Imap ,
103
+ expires_at : DateTime :: < Utc > :: MAX_UTC ,
104
+ imap_server_hostname : "127.0.0.1" . to_string ( ) ,
105
+ imap_server_port : 3993 ,
106
+ imap_password : TEST_SERVER_EMAIL . to_string ( ) ,
107
+ imap_use_tls : true ,
108
+ imap_use_starttls : false ,
109
+ gmail_access_token : String :: new ( ) ,
110
+ gmail_refresh_token : String :: new ( ) ,
111
+ }
112
+ }
93
113
}
94
114
95
115
fn send_notification ( notifications_email_count : & Rc < RefCell < i32 > > , new_messages : Vec < NewMessage > , application_obj : & Application ) {
@@ -132,6 +152,7 @@ mod imp {
132
152
pub application_message_sender : RefCell < Option < glib:: Sender < ApplicationMessage > > > ,
133
153
pub store : RefCell < Option < Rc < services:: Store > > > ,
134
154
pub notifications_email_count : Rc < RefCell < i32 > > ,
155
+ pub test_server_enabled : Rc < RefCell < bool > > ,
135
156
}
136
157
137
158
#[ glib:: object_subclass]
@@ -147,29 +168,27 @@ mod imp {
147
168
fn activate ( & self ) {
148
169
debug ! ( "Application activate" ) ;
149
170
150
- {
171
+ let mut application_message;
172
+ if self . should_set_up_test_server ( ) {
173
+ debug ! ( "Test server setup not found. Configuring" ) ;
174
+
175
+ application_message = ApplicationMessage :: save_identity_message_for_test_server ( ) ;
176
+ } else {
151
177
let store_borrow = self . store . borrow ( ) ;
152
178
let store = store_borrow. as_ref ( ) . expect ( "Unable to access store" ) ;
153
- match store. initialize_database ( ) {
154
- Ok ( _) => {
155
- let application_message = match store. is_account_setup_needed ( ) {
179
+
180
+ application_message = match store. is_account_setup_needed ( ) {
156
181
true => ApplicationMessage :: Setup { } ,
157
182
false => ApplicationMessage :: LoadIdentities { initialize : false } ,
158
- } ;
183
+ }
184
+ }
159
185
160
186
self . application_message_sender
161
187
. borrow ( )
162
188
. as_ref ( )
163
189
. expect ( "Unable to access application message sender" )
164
190
. send ( application_message)
165
191
. expect ( "Unable to send application message" ) ;
166
- }
167
- Err ( e) => {
168
- //@TODO show an error dialog
169
- error ! ( "Error encountered when initializing the database: {}" , & e) ;
170
- }
171
- }
172
- }
173
192
174
193
self . parent_activate ( ) ;
175
194
@@ -184,16 +203,27 @@ mod imp {
184
203
fn startup ( & self ) {
185
204
debug ! ( "Application startup" ) ;
186
205
self . parent_startup ( ) ;
187
- let app = self . obj ( ) ;
188
206
189
- // Set icons for shell
190
207
gtk:: Window :: set_default_icon_name ( APP_ID ) ;
191
208
192
- app. setup_css ( ) ;
193
- app. setup_gactions ( ) ;
194
- app. setup_accels ( ) ;
209
+ let obj = self . obj ( ) ;
210
+ obj. setup_css ( ) ;
211
+ obj. setup_gactions ( ) ;
212
+ obj. setup_accels ( ) ;
195
213
196
214
self . run ( ) ;
215
+ self . setup_database ( ) ;
216
+ }
217
+
218
+ fn command_line ( & self , command_line : & gio:: ApplicationCommandLine ) -> glib:: ExitCode {
219
+ if command_line. options_dict ( ) . contains ( & "with-test-server" ) {
220
+ debug ! ( "Enabling test server setup" ) ;
221
+ self . enable_test_server_setup ( ) ;
222
+ }
223
+
224
+ self . obj ( ) . activate ( ) ;
225
+
226
+ glib:: ExitCode :: SUCCESS
197
227
}
198
228
}
199
229
@@ -569,6 +599,34 @@ mod imp {
569
599
} ) ;
570
600
application_obj. add_action ( & action_show_conversation_for_email_id) ;
571
601
}
602
+
603
+ fn setup_database ( & self ) {
604
+ let store_borrow = self . store . borrow ( ) ;
605
+ let store = store_borrow. as_ref ( ) . expect ( "Unable to access store" ) ;
606
+
607
+ store. initialize_database ( ) ;
608
+ }
609
+
610
+ fn enable_test_server_setup ( & self ) {
611
+ self . test_server_enabled . replace ( true ) ;
612
+ }
613
+
614
+ fn should_set_up_test_server ( & self ) -> bool {
615
+ * self . test_server_enabled . borrow ( ) && !self . is_test_server_set_up_already ( )
616
+ }
617
+
618
+ fn is_test_server_set_up_already ( & self ) -> bool {
619
+ let store_borrow = self . store . borrow ( ) ;
620
+ let store = store_borrow. as_ref ( ) . expect ( "Unable to access store" ) ;
621
+
622
+ store
623
+ . get_bare_identities ( )
624
+ . expect ( "Unable to fetch identities" )
625
+ . into_iter ( )
626
+ . filter ( |identity| & identity. email_address == TEST_SERVER_EMAIL )
627
+ . count ( )
628
+ != 0
629
+ }
572
630
}
573
631
}
574
632
0 commit comments