Commit c1421c3a c1421c3a608c87f900733fe336f807546cac93b1 by Sergey Poznyakoff

mailbox iterator: implement reverse direction and itrctl.

* libmailutils/mailbox/mbxitr.c (mailbox_iterator)<backwards>: New member.
(mbx_first): Position to the last message if itr->backwards is set.
(mbx_next): Decrement index if itr->backwards is set.
(mbx_finished_p): Take into account iteration direction.
(mbx_itrctl): new method.
(mu_mailbox_get_iterator): Set itrctl method.
1 parent 2428c02e
...@@ -175,9 +175,7 @@ _create_mailbox (mu_mailbox_t *pmbox, const char *name) ...@@ -175,9 +175,7 @@ _create_mailbox (mu_mailbox_t *pmbox, const char *name)
175 } 175 }
176 176
177 /* The Mailbox Factory. 177 /* The Mailbox Factory.
178 Create an iterator for registrar and see if any url scheme match, 178 */
179 Then we call the mailbox's mu_url_create() to parse the URL. Last
180 initialize the concrete mailbox and folder. */
181 int 179 int
182 mu_mailbox_create (mu_mailbox_t *pmbox, const char *name) 180 mu_mailbox_create (mu_mailbox_t *pmbox, const char *name)
183 { 181 {
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
27 #include <mailutils/errno.h> 27 #include <mailutils/errno.h>
28 #include <mailutils/error.h> 28 #include <mailutils/error.h>
29 #include <mailutils/iterator.h> 29 #include <mailutils/iterator.h>
30 #include <mailutils/message.h>
31 #include <mailutils/attribute.h>
30 32
31 #include <mailutils/sys/mailbox.h> 33 #include <mailutils/sys/mailbox.h>
32 34
...@@ -34,13 +36,17 @@ struct mailbox_iterator ...@@ -34,13 +36,17 @@ struct mailbox_iterator
34 { 36 {
35 mu_mailbox_t mbx; 37 mu_mailbox_t mbx;
36 size_t idx; 38 size_t idx;
39 int backwards;
37 }; 40 };
38 41
39 static int 42 static int
40 mbx_first (void *owner) 43 mbx_first (void *owner)
41 { 44 {
42 struct mailbox_iterator *itr = owner; 45 struct mailbox_iterator *itr = owner;
43 itr->idx = 1; 46 if (itr->backwards)
47 return mu_mailbox_messages_count (itr->mbx, &itr->idx);
48 else
49 itr->idx = 1;
44 return 0; 50 return 0;
45 } 51 }
46 52
...@@ -48,7 +54,14 @@ static int ...@@ -48,7 +54,14 @@ static int
48 mbx_next (void *owner) 54 mbx_next (void *owner)
49 { 55 {
50 struct mailbox_iterator *itr = owner; 56 struct mailbox_iterator *itr = owner;
51 itr->idx++; 57
58 if (itr->backwards)
59 {
60 if (itr->idx)
61 --itr->idx;
62 }
63 else
64 itr->idx++;
52 return 0; 65 return 0;
53 } 66 }
54 67
...@@ -74,11 +87,17 @@ static int ...@@ -74,11 +87,17 @@ static int
74 mbx_finished_p (void *owner) 87 mbx_finished_p (void *owner)
75 { 88 {
76 struct mailbox_iterator *itr = owner; 89 struct mailbox_iterator *itr = owner;
77 size_t count; 90
78 91 if (itr->backwards)
79 if (mu_mailbox_messages_count (itr->mbx, &count)) 92 return itr->idx == 0;
80 return 1; 93 else
81 return itr->idx > count; 94 {
95 size_t count;
96
97 if (mu_mailbox_messages_count (itr->mbx, &count))
98 return 1;
99 return itr->idx > count;
100 }
82 } 101 }
83 102
84 static int 103 static int
...@@ -114,6 +133,55 @@ mbx_data_dup (void **ptr, void *owner) ...@@ -114,6 +133,55 @@ mbx_data_dup (void **ptr, void *owner)
114 return 0; 133 return 0;
115 } 134 }
116 135
136 static int
137 mbx_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
138 {
139 int rc;
140 struct mailbox_iterator *itr = owner;
141 mu_message_t msg;
142 mu_attribute_t attr;
143
144 if (itr->idx == 0)
145 return MU_ERR_NOENT;
146 switch (req)
147 {
148 case mu_itrctl_tell:
149 *(size_t*)arg = itr->idx;
150 break;
151
152 case mu_itrctl_delete:
153 rc = mu_mailbox_get_message (itr->mbx, itr->idx, &msg);
154 if (rc)
155 return rc;
156 rc = mu_message_get_attribute (msg, &attr);
157 if (rc)
158 return rc;
159 rc = mu_attribute_set_deleted (attr);
160 if (rc)
161 return rc;
162 break;
163
164 case mu_itrctl_qry_direction:
165 if (!arg)
166 return EINVAL;
167 else
168 *(int*)arg = itr->backwards;
169 break;
170
171 case mu_itrctl_set_direction:
172 if (!arg)
173 return EINVAL;
174 else
175 itr->backwards = !!*(int*)arg;
176 break;
177
178 default:
179 return ENOSYS;
180 }
181 return 0;
182 }
183
184
117 int 185 int
118 mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator) 186 mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
119 { 187 {
...@@ -129,7 +197,8 @@ mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator) ...@@ -129,7 +197,8 @@ mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
129 return ENOMEM; 197 return ENOMEM;
130 itr->mbx = mbx; 198 itr->mbx = mbx;
131 itr->idx = 1; 199 itr->idx = 1;
132 200 itr->backwards = 0;
201
133 status = mu_iterator_create (&iterator, itr); 202 status = mu_iterator_create (&iterator, itr);
134 if (status) 203 if (status)
135 { 204 {
...@@ -144,6 +213,7 @@ mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator) ...@@ -144,6 +213,7 @@ mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
144 mu_iterator_set_curitem_p (iterator, mbx_curitem_p); 213 mu_iterator_set_curitem_p (iterator, mbx_curitem_p);
145 mu_iterator_set_destroy (iterator, mbx_destroy); 214 mu_iterator_set_destroy (iterator, mbx_destroy);
146 mu_iterator_set_dup (iterator, mbx_data_dup); 215 mu_iterator_set_dup (iterator, mbx_data_dup);
216 mu_iterator_set_itrctl (iterator, mbx_itrctl);
147 217
148 mu_iterator_attach (&mbx->iterator, iterator); 218 mu_iterator_attach (&mbx->iterator, iterator);
149 219
......