]> code.citadel.org Git - citadel.git/blobdiff - webcit/sieve.c
Added a 'Delete Rule' button
[citadel.git] / webcit / sieve.c
index ab6050ce1f0862b7c53b89b34f4d6da8985e5169..ba982d328caf185474c5e7b9837e8c6dbc558b2c 100644 (file)
@@ -9,7 +9,7 @@
 #include "webcit.h"
 
 #define MAX_SCRIPTS    100
-#define MAX_RULES      8
+#define MAX_RULES      10
 
 /**
  * \brief view/edit sieve config
@@ -372,49 +372,120 @@ void create_script(void) {
 
 
 void display_rules_editor_inner_div(void) {
-       int i;
+       int i, j;
+       char buf[256];
+
+       struct {
+               char name[128];
+       } *rooms = NULL;
+       int num_roomnames = 0;
+       int num_roomnames_alloc = 0;
+
+
+       /* load the roomnames */
+       serv_puts("LKRA");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '1') {
+               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                       ++num_roomnames;
+                       if (num_roomnames > num_roomnames_alloc) {
+                               num_roomnames_alloc += 250;
+                               rooms = realloc(rooms, (num_roomnames_alloc * 128));
+                       }
+                       extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
+               }
+       }
+
 
 /*
  * This script should get called by every onChange event...
  *
  */
        wprintf("<script type=\"text/javascript\">                                      \n"
+               "                                                                       \n"
+               "var highest_active_rule = (-1);                                        \n"
                "                                                                       \n"
                "function UpdateRules() {                                               \n"
                "  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
-       wprintf("  d = ($('action'+i).options[$('action'+i).selectedIndex].value);      \n"
-               "  if (d == 'fileinto') {                                               \n"
-               "    $('div_fileinto'+i).style.display = 'block';                       \n"
-               "    $('div_redirect'+i).style.display = 'none';                        \n"
-               "  } else if (d == 'redirect') {                                        \n"
-               "    $('div_fileinto'+i).style.display = 'none';                        \n"
+       wprintf("    d = ($('movedown'+i));                                             \n"
+               "    if (i < highest_active_rule) {                                     \n"
+               "      d.style.display = 'block';                                       \n"
+               "    }                                                                  \n"
+               "    else {                                                             \n"
+               "      d.style.display = 'none';                                        \n"
+               "    }                                                                  \n"
+               "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
+               "    if (d == 'fileinto') {                                             \n"
+               "      $('div_fileinto'+i).style.display = 'block';                     \n"
+               "      $('div_redirect'+i).style.display = 'none';                      \n"
+               "    } else if (d == 'redirect') {                                      \n"
+               "      $('div_fileinto'+i).style.display = 'none';                      \n"
                "    $('div_redirect'+i).style.display = 'block';                       \n"
-               "  } else  {                                                            \n"
-               "    $('div_fileinto'+i).style.display = 'none';                        \n"
-               "    $('div_redirect'+i).style.display = 'none';                        \n"
+               "    } else  {                                                          \n"
+               "      $('div_fileinto'+i).style.display = 'none';                      \n"
+               "      $('div_redirect'+i).style.display = 'none';                      \n"
+               "    }                                                                  \n"
                "  }                                                                    \n"
-               " }                                                                     \n"
-               "}                                                                      \n"
-               "</script>                                                              \n"
        );
-
-
 /*
- * Show/hide alternating rows.  This is obviously bogus, it's just here to test the show/hide logic.
+ * Show only the active rows...
+ */
+       wprintf("  highest_active_rule = (-1);                                          \n");
        wprintf("  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
-       wprintf("   if ( (i % 2) == 0 )  {                                              \n"
+       wprintf("   if ($('active'+i).checked) {                                        \n"
                "     $('rule' + i).style.display = 'block';                            \n"
+               "     highest_active_rule = i;                                          \n"
                "   }                                                                   \n"
                "   else {                                                              \n"
                "     $('rule' + i).style.display = 'none';                             \n"
                "   }                                                                   \n"
                "  }                                                                    \n"
                "}                                                                      \n"
+/*
+ * Add a rule (really, just un-hide it)
+ * FIXME check the upper bound
+ */
+               "function AddRule() {                                                   \n"
+               "  highest_active_rule = highest_active_rule + 1;                       \n"
+               "  $('active'+highest_active_rule).checked = true;                      \n"
+               "  UpdateRules();                                                       \n"
+               "}                                                                      \n"
+/*
+ * Swap two rules
+ */
+               "function SwapRules(ra, rb) {                                           \n"
+               "                                                                       \n"
+               "  var things = new Array();                                            \n"
+               "  things[0] = 'hfield';                                                \n"
+               "  things[1] = 'compare';                                               \n"
+               "  things[2] = 'htext';                                                 \n"
+               "  things[3] = 'action';                                                \n"
+               "  things[4] = 'fileinto';                                              \n"
+               "  things[5] = 'redirect';                                              \n"
+               "  things[6] = 'final';                                                 \n"
                "                                                                       \n"
-*/
+               "  for (i=0; i<7; ++i) {                                                \n"
+               "    tempval=$(things[i]+ra).value;                                     \n"
+               "    $(things[i]+ra).value = $(things[i]+rb).value;                     \n"
+               "    $(things[i]+rb).value = tempval;                                   \n"
+               "  }                                                                    \n"
+               "}                                                                      \n"
+/*
+ * Delete a rule (percolate the deleted rule out to the end,
+ *                and then decrement highest_active_rule)
+ */
+               "function DeleteRule(rd) {                                              \n"
+               "  for (i=rd; i<highest_active_rule; ++i) {                             \n"
+               "    SwapRules(i, (i+1));                                               \n"
+               "  }                                                                    \n"
+               "  $('active'+highest_active_rule).checked = false;                     \n"
+               "}                                                                      \n"
+               "</script>                                                              \n"
+       );
 
 
        wprintf("<br />");
+
        wprintf("<table class=\"mailbox_summary\" rules=rows cellpadding=2 "
                "style=\"width:100%%;-moz-user-select:none;\">"
        );
@@ -423,24 +494,42 @@ void display_rules_editor_inner_div(void) {
                
                wprintf("<tr id=\"rule%d\">", i);
 
-               wprintf("<td>%d. %s</td>", i+1, _("If") );
+               wprintf("<td>");
+
+               wprintf("<div style=\"display:none\">");
+               wprintf("<input type=\"checkbox\" id=\"active%d\">", i);
+               wprintf("</div>");
+
+               if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
+                       "<img border=\"0\" src=\"static/up_pointer.gif\" /></a>", i-1, i);
+
+               wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
+                       "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" /></a>",
+                       i, i+1, i);
+
+               wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
+                       "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" /></a>",
+                       i, i);
+
+               wprintf("&nbsp;%d.&nbsp;%s</td>", i+1, _("If") );
 
                wprintf("<td>");
-               wprintf("<select name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">", i);
+
+               wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
+                       i, i);
                wprintf("<option value=\"sender\">%s</option>", _("Sender"));
                wprintf("<option value=\"recipient\">%s</option>", _("Recipient"));
                wprintf("</select>");
                wprintf("</td>");
 
                wprintf("<td>");
-               wprintf("<select name=\"compare%d\" size=1 onChange=\"UpdateRules();\">", i);
+               wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
+                       i, i);
                wprintf("<option value=\"match\">%s</option>", _("matches"));
                wprintf("<option value=\"notmatch\">%s</option>", _("does not match"));
                wprintf("</select>");
-               wprintf("</td>");
 
-               wprintf("<td>");
-               wprintf("<input type=\"text\" name=\"htext%d\">", i);
+               wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\">", i, i);
                wprintf("</td>");
 
                wprintf("<td>");
@@ -452,11 +541,24 @@ void display_rules_editor_inner_div(void) {
                wprintf("</select>");
 
                wprintf("<div id=\"div_fileinto%d\">", i);
-               wprintf("<input type=\"text\" name=\"fileinto%d\">", i);
+               //wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\" style=\"width:100px\">", i, i);
+               wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
+               for (j=0; j<num_roomnames; ++j) {
+                       wprintf("<option ");
+                       if (!strcasecmp(rooms[j].name, "Mail")) {
+                               wprintf("selected ");
+                       }
+                       wprintf("value=\"");
+                       urlescputs(rooms[j].name);
+                       wprintf("\">");
+                       escputs(rooms[j].name);
+                       wprintf("</option>\n");
+               }
+               wprintf("</select>\n");
                wprintf("</div>");
 
                wprintf("<div id=\"div_redirect%d\">", i);
-               wprintf("<input type=\"text\" name=\"redirect%d\">", i);
+               wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\">", i, i);
                wprintf("</div>");
                wprintf("</td>");
 
@@ -465,7 +567,8 @@ void display_rules_editor_inner_div(void) {
                wprintf("<td>%s</td>", _("and then") );
 
                wprintf("<td>");
-               wprintf("<select name=\"final%d\" size=1 onChange=\"UpdateRules();\">", i);
+               wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
+                       i, i);
                wprintf("<option value=\"stop\">%s</option>", _("stop"));
                wprintf("<option value=\"continue\">%s</option>", _("continue processing"));
                wprintf("</select>");
@@ -476,15 +579,14 @@ void display_rules_editor_inner_div(void) {
        }
 
        wprintf("</table>");
-
-
-
+       wprintf("<a href=\"javascript:AddRule();\">Add rule</a><br />\n");
 
        wprintf("<script type=\"text/javascript\">                                      \n"
                "UpdateRules();                                                         \n"
                "</script>                                                              \n"
        );
 
+       free(rooms);
 }