Introduce SpeedDialError and test invalid save messages

This commit is contained in:
Christian Dywan 2012-09-12 21:18:56 +02:00
parent 55c06a4285
commit 13c884b5cb
3 changed files with 60 additions and 20 deletions

View File

@ -18,6 +18,16 @@ namespace Sokoke {
} }
namespace Midori { namespace Midori {
public errordomain SpeedDialError {
INVALID_MESSAGE,
NO_ACTION,
NO_ID,
NO_URL,
NO_TITLE,
NO_ID2,
INVALID_ACTION,
}
public class SpeedDial : GLib.Object { public class SpeedDial : GLib.Object {
string filename; string filename;
string? html = null; string? html = null;
@ -108,7 +118,7 @@ namespace Midori {
} }
} }
public string get_next_free_slot () { public string get_next_free_slot (out uint count = null) {
uint slot_count = 0; uint slot_count = 0;
foreach (string tile in keyfile.get_groups ()) { foreach (string tile in keyfile.get_groups ()) {
try { try {
@ -117,14 +127,17 @@ namespace Midori {
} }
catch (KeyFileError error) { } catch (KeyFileError error) { }
} }
if (&count != null)
count = slot_count;
uint slot = 1; uint slot = 1;
while (slot <= slot_count) { while (slot <= slot_count) {
string tile = "Dial %u".printf (slot); string tile = "Dial %u".printf (slot);
if (!keyfile.has_group (tile)) if (!keyfile.has_group (tile))
return "Dial %u".printf (slot); return tile;
slot++; slot++;
} }
return "Dial %u".printf (slot_count + 1); return "Dial %u".printf (slot_count + 1);
} }
@ -172,13 +185,8 @@ namespace Midori {
var markup = new StringBuilder (header); var markup = new StringBuilder (header);
uint slot_count = 1; uint slot_count = 1;
foreach (string tile in keyfile.get_groups ()) { string dial_id = get_next_free_slot (out slot_count);
try { uint next_slot = dial_id.substring (5, -1).to_int ();
if (keyfile.has_key (tile, "uri"))
slot_count++;
}
catch (KeyFileError error) { }
}
/* Try to guess the best X by X grid size */ /* Try to guess the best X by X grid size */
uint grid_index = 3; uint grid_index = 3;
@ -250,7 +258,7 @@ namespace Midori {
<a class="add" href="#" onclick='return getAction("%u");'></a> <a class="add" href="#" onclick='return getAction("%u");'></a>
</div><div class="title">%s</div></div> </div><div class="title">%s</div></div>
""", """,
slot_count + 1, slot_count + 1, _("Click to add a shortcut")); next_slot, next_slot, _("Click to add a shortcut"));
markup.append_printf ("</div>\n</body>\n</html>\n"); markup.append_printf ("</div>\n</body>\n</html>\n");
html = markup.str; html = markup.str;
} }
@ -261,14 +269,18 @@ namespace Midori {
} }
public void save_message (string message) throws Error { public void save_message (string message) throws Error {
if (!message.has_prefix ("speed_dial-save-"))
throw new SpeedDialError.INVALID_MESSAGE ("Invalid message '%s'", message);
string msg = message.substring (16, -1); string msg = message.substring (16, -1);
string[] parts = msg.split (" ", 4); string[] parts = msg.split (" ", 4);
if (parts[0] == null)
throw new SpeedDialError.NO_ACTION ("No action.");
string action = parts[0]; string action = parts[0];
if (action == "add" || action == "rename" if (parts[1] == null)
|| action == "delete" || action == "swap") { throw new SpeedDialError.NO_ID ("No ID argument.");
uint slot_id = parts[1].to_int () ; string dial_id = "Dial " + parts[1];
string dial_id = "Dial %u".printf (slot_id);
if (action == "delete") { if (action == "delete") {
string uri = keyfile.get_string (dial_id, "uri"); string uri = keyfile.get_string (dial_id, "uri");
@ -277,17 +289,21 @@ namespace Midori {
FileUtils.unlink (file_path); FileUtils.unlink (file_path);
} }
else if (action == "add") { else if (action == "add") {
if (parts[2] == null)
throw new SpeedDialError.NO_URL ("No URL argument.");
keyfile.set_string (dial_id, "uri", parts[2]); keyfile.set_string (dial_id, "uri", parts[2]);
get_thumb (dial_id, parts[2]); get_thumb (dial_id, parts[2]);
} }
else if (action == "rename") { else if (action == "rename") {
uint offset = parts[0].length + parts[1].length + 2; if (parts[2] == null)
string title = msg.substring (offset, -1); throw new SpeedDialError.NO_TITLE ("No title argument.");
string title = parts[2];
keyfile.set_string (dial_id, "title", title); keyfile.set_string (dial_id, "title", title);
} }
else if (action == "swap") { else if (action == "swap") {
uint slot2_id = parts[2].to_int (); if (parts[2] == null)
string dial2_id = "Dial %u".printf (slot2_id); throw new SpeedDialError.NO_ID2 ("No ID2 argument.");
string dial2_id = "Dial " + parts[2];
string uri = keyfile.get_string (dial_id, "uri"); string uri = keyfile.get_string (dial_id, "uri");
string title = keyfile.get_string (dial_id, "title"); string title = keyfile.get_string (dial_id, "title");
@ -299,7 +315,9 @@ namespace Midori {
keyfile.set_string (dial_id, "title", title2); keyfile.set_string (dial_id, "title", title2);
keyfile.set_string (dial2_id, "title", title); keyfile.set_string (dial2_id, "title", title);
} }
} else
throw new SpeedDialError.INVALID_ACTION ("Invalid action '%s'", action);
save (); save ();
} }

View File

@ -3312,7 +3312,13 @@ webkit_web_view_console_message_cb (GtkWidget* web_view,
{ {
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view)); MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial"); MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
midori_speed_dial_save_message (dial, message, NULL); GError* error = NULL;
midori_speed_dial_save_message (dial, message, &error);
if (error != NULL)
{
g_critical ("Failed speed dial message: %s\n", error->message);
g_error_free (error);
}
} }
else else
g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id); g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id);

View File

@ -43,6 +43,22 @@ static void speeddial_load () {
Katze.assert_str_equal (json, dial_data.get_next_free_slot (), "Dial 2"); Katze.assert_str_equal (json, dial_data.get_next_free_slot (), "Dial 2");
Katze.assert_str_equal (json, dial_json.get_next_free_slot (), "Dial 2"); Katze.assert_str_equal (json, dial_json.get_next_free_slot (), "Dial 2");
try {
dial_data.save_message ("SpeedDial");
assert_not_reached ();
}
catch (Error error) { /* Error expected: pass */ }
try {
dial_data.save_message ("speed_dial-save-rename ");
assert_not_reached ();
}
catch (Error error) { /* Error expected: pass */ }
try {
dial_data.save_message ("speed_dial-save-foo 1");
assert_not_reached ();
}
catch (Error error) { /* Error expected: pass */ }
dial_data.save_message ("speed_dial-save-rename 1 Lorem"); dial_data.save_message ("speed_dial-save-rename 1 Lorem");
Katze.assert_str_equal (data, dial_data.keyfile.get_string ("Dial 1", "title"), "Lorem"); Katze.assert_str_equal (data, dial_data.keyfile.get_string ("Dial 1", "title"), "Lorem");
dial_data.save_message ("speed_dial-save-delete 1"); dial_data.save_message ("speed_dial-save-delete 1");