Browse code

Refactor inbox message handling

Natasha Kerensikova authored on25/04/2016 19:33:52
Showing1 changed files
... ...
@@ -430,15 +430,13 @@ handle_last_sent(Tuple *tuple) {
430 430
 }
431 431
 
432 432
 static void
433
-inbox_received_handler(DictionaryIterator *iterator, void *context) {
434
-	Tuple *tuple;
435
-	(void)context;
433
+handle_received_tuple(Tuple *tuple) {
434
+	switch (tuple->key) {
435
+	    case MSG_KEY_LAST_SENT:
436
+		handle_last_sent (tuple);
437
+		break;
436 438
 
437
-	tuple = dict_find(iterator, MSG_KEY_LAST_SENT);
438
-	if (tuple) handle_last_sent (tuple);
439
-
440
-	tuple = dict_find(iterator, MSG_KEY_MODAL_MESSAGE);
441
-	if (tuple) {
439
+	    case MSG_KEY_MODAL_MESSAGE:
442 440
 		if (tuple->type != TUPLE_CSTRING) {
443 441
 			APP_LOG(APP_LOG_LEVEL_ERROR,
444 442
 			    "Unexpected type %d for MSG_KEY_MODAL_MESSAGE",
... ...
@@ -447,48 +445,60 @@ inbox_received_handler(DictionaryIterator *iterator, void *context) {
447 445
 			set_modal_mode(true);
448 446
 			set_modal_message(tuple->value->cstring);
449 447
 		}
450
-	}
448
+		break;
451 449
 
452
-	tuple = dict_find(iterator, MSG_KEY_UPLOAD_DONE);
453
-	if (tuple) {
450
+	    case MSG_KEY_UPLOAD_DONE:
454 451
 		web.current_key = tuple_uint(tuple);
455 452
 		if (!web.first_key) web.first_key = web.current_key;
456 453
 		display_dirty = true;
457 454
 		if (auto_close && !sending_data
458 455
 		    && web.current_key >= phone.current_key)
459 456
 			close_app();
460
-	}
457
+		break;
461 458
 
462
-	tuple = dict_find(iterator, MSG_KEY_UPLOAD_START);
463
-	if (tuple) {
459
+	    case MSG_KEY_UPLOAD_START:
464 460
 		web.first_key = tuple_uint(tuple);
465 461
 		web.start_time = time(0);
466
-	}
462
+		break;
467 463
 
468
-	tuple = dict_find(iterator, MSG_KEY_CFG_AUTO_CLOSE);
469
-	if (tuple) {
464
+	    case MSG_KEY_CFG_AUTO_CLOSE:
470 465
 		auto_close = cfg_auto_close = (tuple_uint(tuple) != 0);
471 466
 		persist_write_bool(MSG_KEY_CFG_AUTO_CLOSE, auto_close);
472 467
 		if (auto_close && !sending_data
473 468
 		    && web.current_key >= phone.current_key)
474 469
 			close_app();
475
-	}
470
+		break;
476 471
 
477
-	tuple = dict_find(iterator, MSG_KEY_CFG_START);
478
-	if (tuple) {
472
+	    case MSG_KEY_CFG_START:
479 473
 		APP_LOG(APP_LOG_LEVEL_INFO, "Starting configuration");
480 474
 		auto_close = false;
481 475
 		configuring = true;
482
-	}
476
+		break;
483 477
 
484
-	tuple = dict_find(iterator, MSG_KEY_CFG_END);
485
-	if (tuple) {
478
+	    case MSG_KEY_CFG_END:
486 479
 		APP_LOG(APP_LOG_LEVEL_INFO, "End of configuration");
487 480
 		auto_close = cfg_auto_close;
488 481
 		configuring = false;
482
+		break;
483
+
484
+	    default:
485
+		APP_LOG(APP_LOG_LEVEL_ERROR,
486
+		    "Unknown key %lu in received message",
487
+		    (unsigned long)tuple->key);
489 488
 	}
490 489
 }
491 490
 
491
+static void
492
+inbox_received_handler(DictionaryIterator *iterator, void *context) {
493
+	Tuple *tuple;
494
+	(void)context;
495
+
496
+	for (tuple = dict_read_first(iterator);
497
+	    tuple;
498
+	    tuple = dict_read_next(iterator))
499
+		handle_received_tuple(tuple);
500
+}
501
+
492 502
 static void
493 503
 outbox_sent_handler(DictionaryIterator *iterator, void *context) {
494 504
 	(void)iterator;