]> git.tuebingen.mpg.de Git - paraslash.git/blob - mood.c
Merge tag 'v0.6.4'
[paraslash.git] / mood.c
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file mood.c Paraslash's mood handling functions. */
4
5 #include <regex.h>
6 #include <osl.h>
7 #include <lopsub.h>
8
9 #include "para.h"
10 #include "error.h"
11 #include "string.h"
12 #include "afh.h"
13 #include "afs.h"
14 #include "list.h"
15 #include "mood.h"
16
17 /*
18  * Mood parser API. It's overkill to have an own header file for
19  * these declarations as they are only needed in this .c file.
20  */
21 struct mp_context;
22 int mp_init(const char *definition, int nbytes, struct mp_context **result,
23                  char **errmsg);
24 bool mp_eval_row(const struct osl_row *aft_row, struct mp_context *ctx);
25 void mp_shutdown(struct mp_context *ctx);
26
27 /**
28  * Contains statistical data of the currently admissible audio files.
29  *
30  * It is used to assign normalized score values to each admissible audio file.
31  */
32 struct afs_statistics {
33         /** Sum of num played over all admissible files. */
34         int64_t num_played_sum;
35         /** Sum of last played times over all admissible files. */
36         int64_t last_played_sum;
37         /** Quadratic deviation of num played count. */
38         int64_t num_played_qd;
39         /** Quadratic deviation of last played time. */
40         int64_t last_played_qd;
41         /** Correction factor for the num played score. */
42         int64_t num_played_correction;
43         /** Correction factor for the last played score. */
44         int64_t last_played_correction;
45         /** Common divisor of the correction factors. */
46         int64_t normalization_divisor;
47         /** Number of admissible files */
48         unsigned num;
49 };
50 static struct afs_statistics statistics = {.normalization_divisor = 1};
51
52 struct mood {
53         /** The name of this mood. */
54         char *name;
55         /** Info for the bison parser. */
56         struct mp_context *parser_context;
57 };
58
59 /*
60  * If current_mood is NULL then no mood is currently open. If
61  * current_mood->name is NULL, the dummy mood is currently open.
62  */
63 static struct mood *current_mood;
64
65 /*
66  * Find the position of the most-significant set bit.
67  *
68  * Copied and slightly adapted from the linux source tree, version 4.9.39
69  * (2017-07).
70  */
71 __a_const static uint32_t fls64(uint64_t v)
72 {
73         int n = 63;
74         const uint64_t ones = ~(uint64_t)0U;
75
76         if ((v & (ones << 32)) == 0) {
77                 n -= 32;
78                 v <<= 32;
79         }
80         if ((v & (ones << (64 - 16))) == 0) {
81                 n -= 16;
82                 v <<= 16;
83         }
84         if ((v & (ones << (64 - 8))) == 0) {
85                 n -= 8;
86                 v <<= 8;
87         }
88         if ((v & (ones << (64 - 4))) == 0) {
89                 n -= 4;
90                 v <<= 4;
91         }
92         if ((v & (ones << (64 - 2))) == 0) {
93                 n -= 2;
94                 v <<= 2;
95         }
96         if ((v & (ones << (64 - 1))) == 0)
97                 n -= 1;
98         return n;
99 }
100
101 /*
102  * Compute the integer square root floor(sqrt(x)).
103  *
104  * Taken 2007 from the linux source tree.
105  */
106 __a_const static uint64_t int_sqrt(uint64_t x)
107 {
108         uint64_t op = x, res = 0, one = 1;
109
110         one = one << (fls64(x) & ~one);
111         while (one != 0) {
112                 if (op >= res + one) {
113                         op = op - (res + one);
114                         res = res + 2 * one;
115                 }
116                 res /= 2;
117                 one /= 4;
118         }
119         return res;
120 }
121
122 /* returns 1 if row admissible, 0 if not, negative on errors */
123 static int row_is_admissible(const struct osl_row *aft_row, struct mood *m)
124 {
125         if (!m)
126                 return -E_NO_MOOD;
127         return mp_eval_row(aft_row, m->parser_context);
128 }
129
130 static void destroy_mood(struct mood *m)
131 {
132         if (!m)
133                 return;
134         mp_shutdown(m->parser_context);
135         free(m->name);
136         free(m);
137 }
138
139 static struct mood *alloc_new_mood(const char *name)
140 {
141         struct mood *m = para_calloc(sizeof(struct mood));
142         if (name)
143                 m->name = para_strdup(name);
144         return m;
145 }
146
147 static int load_mood(const struct osl_row *mood_row, struct mood **m,
148                 char **errmsg)
149 {
150         char *mood_name;
151         struct osl_object mood_def;
152         int ret;
153
154         ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
155         if (ret < 0) {
156                 if (errmsg)
157                         *errmsg = make_message(
158                                 "could not read mood definition");
159                 return ret;
160         }
161         assert(*mood_name);
162         *m = alloc_new_mood(mood_name);
163         PARA_INFO_LOG("opening mood %s\n", mood_name);
164         ret = mp_init(mood_def.data, mood_def.size, &(*m)->parser_context, errmsg);
165         osl_close_disk_object(&mood_def);
166         return ret;
167 }
168
169 static int check_mood(struct osl_row *mood_row, void *data)
170 {
171         struct para_buffer *pb = data;
172         char *mood_name, *errmsg;
173         struct osl_object mood_def;
174         struct mood *m;
175         int ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
176
177         if (ret < 0) {
178                 para_printf(pb, "cannot read mood\n");
179                 return ret;
180         }
181         if (!*mood_name) /* ignore dummy row */
182                 goto out;
183         m = alloc_new_mood("check");
184         ret = mp_init(mood_def.data, mood_def.size, &m->parser_context,
185                 &errmsg);
186         if (ret < 0) {
187                 para_printf(pb, "%s: %s\n", mood_name, errmsg);
188                 free(errmsg);
189                 para_printf(pb, "%s\n", para_strerror(-ret));
190         } else
191                 destroy_mood(m);
192         ret = 1; /* don't fail the loop on invalid mood definitions */
193 out:
194         osl_close_disk_object(&mood_def);
195         return ret;
196 }
197
198 /**
199  * Check all moods for syntax errors.
200  *
201  * \param aca Only ->pbout is used for diagnostics.
202  *
203  * \return Negative on fatal errors. Inconsistent mood definitions are not
204  * considered an error.
205  */
206 int mood_check_callback(struct afs_callback_arg *aca)
207 {
208         para_printf(&aca->pbout, "checking moods...\n");
209         return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, &aca->pbout,
210                 check_mood));
211 }
212
213 /*
214  * The normalized num_played and last_played values are defined as
215  *
216  *      nn := -(np - mean_n) / sigma_n and nl := -(lp - mean_l) / sigma_l
217  *
218  *  For a (hypothetical) file with np = 0 and lp = now we thus have
219  *
220  *      nn =  mean_n / sigma_n =: hn > 0
221  *      nl = -(now - mean_l) / sigma_l =: hl < 0
222  *
223  * We design the score function so that both contributions get the same
224  * weight. Define the np and lp score of an arbitrary file as
225  *
226  *      sn := nn * -hl and sl := nl * hn
227  *
228  * Example:
229  *      num_played mean/sigma: 87/14
230  *      last_played mean/sigma: 45/32 days
231  *
232  *      We have hn = 87 / 14 = 6.21 and hl = -45 / 32 = -1.41. Multiplying
233  *      nn of every file with the correction factor 1.41 and nl with
234  *      6.21 makes the weight of the two contributions equal.
235  *
236  * The total score s := sn + sl has the representation
237  *
238  *      s = -cn * (np - mean_n) - cl * (lp - mean_l)
239  *
240  * with positive correction factors
241  *
242  *      cn = (now - mean_l) / (sqrt(ql) * sqrt(qn) / n)
243  *      cl = mean_n / (sqrt(ql) * sqrt(qn) / n)
244  *
245  * where ql and qn are the quadratic deviations stored in the statistics
246  * structure and n is the number of admissible files. To avoid integer
247  * overflows and rounding errors we store the common divisor of the
248  * correction factors separately.
249  */
250 static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
251 {
252         if (!n || !qd)
253                 return 0;
254         return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
255 }
256
257 static long compute_score(struct afs_info *afsi)
258 {
259         long score = -normalized_value(afsi->num_played, statistics.num,
260                 statistics.num_played_sum, statistics.num_played_qd);
261         score -= normalized_value(afsi->last_played, statistics.num,
262                 statistics.last_played_sum, statistics.last_played_qd);
263         return score / 2;
264 }
265
266 static int add_afs_statistics(const struct osl_row *row)
267 {
268         uint64_t n, x, s, q;
269         struct afs_info afsi;
270         int ret;
271
272         ret = get_afsi_of_row(row, &afsi);
273         if (ret < 0)
274                 return ret;
275         n = statistics.num;
276         x = afsi.last_played;
277         s = statistics.last_played_sum;
278         if (n > 0) {
279                 q = (x > s / n)? x - s / n : s / n - x;
280                 statistics.last_played_qd += q * q * n / (n + 1);
281         }
282         statistics.last_played_sum += x;
283
284         x = afsi.num_played;
285         s = statistics.num_played_sum;
286         if (n > 0) {
287                 q = (x > s / n)? x - s / n : s / n - x;
288                 statistics.num_played_qd += q * q * n / (n + 1);
289         }
290         statistics.num_played_sum += x;
291         statistics.num++;
292         return 1;
293 }
294
295 static int del_afs_statistics(const struct osl_row *row)
296 {
297         uint64_t n, s, q, a, new_s;
298         struct afs_info afsi;
299         int ret;
300         ret = get_afsi_of_row(row, &afsi);
301         if (ret < 0)
302                 return ret;
303         n = statistics.num;
304         assert(n);
305         if (n == 1) {
306                 memset(&statistics, 0, sizeof(statistics));
307                 statistics.normalization_divisor = 1;
308                 return 1;
309         }
310
311         s = statistics.last_played_sum;
312         q = statistics.last_played_qd;
313         a = afsi.last_played;
314         new_s = s - a;
315         statistics.last_played_sum = new_s;
316         statistics.last_played_qd = q + s * s / n - a * a
317                 - new_s * new_s / (n - 1);
318
319         s = statistics.num_played_sum;
320         q = statistics.num_played_qd;
321         a = afsi.num_played;
322         new_s = s - a;
323         statistics.num_played_sum = new_s;
324         statistics.num_played_qd = q + s * s / n - a * a
325                 - new_s * new_s / (n - 1);
326
327         statistics.num--;
328         return 1;
329 }
330
331 /*
332  * At mood open time we determine the set of admissible files for the given
333  * mood where each file is identified by a pointer to a row of the audio file
334  * table. In the first pass the pointers are added to a temporary array and
335  * statistics are computed. When all admissible files have been processed in
336  * this way, the score of each admissible file is computed and the (row, score)
337  * pair is added to the score table. This has to be done in a second pass
338  * since the score depends on the statistics. Finally, the array is freed.
339  */
340 struct admissible_array {
341         /** Files are admissible wrt. this mood. */
342         struct mood *m;
343         /** The size of the array */
344         unsigned size;
345         /** Pointer to the array of admissible files. */
346         struct osl_row **array;
347 };
348
349 /*
350  * Check whether the given audio file is admissible. If it is, add it to array
351  * of admissible files.
352  */
353 static int add_if_admissible(struct osl_row *aft_row, void *data)
354 {
355         struct admissible_array *aa = data;
356         int ret;
357
358         ret = row_is_admissible(aft_row, aa->m);
359         if (ret <= 0)
360                 return ret;
361         if (statistics.num >= aa->size) {
362                 aa->size *= 2;
363                 aa->size += 100;
364                 aa->array = para_realloc(aa->array,
365                         aa->size * sizeof(struct osl_row *));
366         }
367         aa->array[statistics.num] = aft_row;
368         return add_afs_statistics(aft_row);
369 }
370
371 /**
372  * Compute the new quadratic deviation in case one element changes.
373  *
374  * \param n Number of elements.
375  * \param old_qd The quadratic deviation before the change.
376  * \param old_val The value that was replaced.
377  * \param new_val The replacement value.
378  * \param old_sum The sum of all elements before the update.
379  *
380  * \return The new quadratic deviation resulting from replacing old_val
381  * by new_val.
382  *
383  * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
384  * their quadratic deviation
385  *
386  * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
387  *
388  * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
389  * the last number a_n was replaced by b) may be computed in O(1) time in terms
390  * of n, q, a_n, b, and S as
391  *
392  *      q' = q + d * s - (2 * S + d) * d / n
393  *         = q + d * (s - 2 * S / n - d /n),
394  *
395  * where d = b - a_n, and s = b + a_n.
396  *
397  * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
398  * s = 17, so
399  *
400  *      q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
401  *
402  * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
403  *
404  */
405 _static_inline_ int64_t update_quadratic_deviation(int64_t n, int64_t old_qd,
406                 int64_t old_val, int64_t new_val, int64_t old_sum)
407 {
408         int64_t delta = new_val - old_val;
409         int64_t sigma = new_val + old_val;
410         return old_qd + delta * (sigma - 2 * old_sum / n - delta / n);
411 }
412
413 static int update_afs_statistics(struct afs_info *old_afsi,
414                 struct afs_info *new_afsi)
415 {
416         unsigned n;
417         int ret = get_num_admissible_files(&n);
418
419         if (ret < 0)
420                 return ret;
421         assert(n);
422
423         statistics.last_played_qd = update_quadratic_deviation(n,
424                 statistics.last_played_qd, old_afsi->last_played,
425                 new_afsi->last_played, statistics.last_played_sum);
426         statistics.last_played_sum += new_afsi->last_played - old_afsi->last_played;
427
428         statistics.num_played_qd = update_quadratic_deviation(n,
429                 statistics.num_played_qd, old_afsi->num_played,
430                 new_afsi->num_played, statistics.num_played_sum);
431         statistics.num_played_sum += new_afsi->num_played - old_afsi->num_played;
432         return 1;
433 }
434
435 static int add_to_score_table(const struct osl_row *aft_row)
436 {
437         long score;
438         struct afs_info afsi;
439         int ret = get_afsi_of_row(aft_row, &afsi);
440
441         if (ret < 0)
442                 return ret;
443         score = compute_score(&afsi);
444         return score_add(aft_row, score);
445 }
446
447 static int delete_from_statistics_and_score_table(const struct osl_row *aft_row)
448 {
449         int ret = del_afs_statistics(aft_row);
450         if (ret < 0)
451                 return ret;
452         return score_delete(aft_row);
453 }
454
455 /**
456  * Delete one entry from the statistics and from the score table.
457  *
458  * \param aft_row The audio file which is no longer admissible.
459  *
460  * \return Positive on success, negative on errors.
461  *
462  * \sa \ref score_delete().
463  */
464 static int mood_delete_audio_file(const struct osl_row *aft_row)
465 {
466         int ret;
467
468         ret = row_belongs_to_score_table(aft_row, NULL);
469         if (ret < 0)
470                 return ret;
471         if (!ret) /* not admissible, nothing to do */
472                 return 1;
473         return delete_from_statistics_and_score_table(aft_row);
474 }
475
476 /**
477  * Compute the new score of an audio file wrt. the current mood.
478  *
479  * \param aft_row Determines the audio file.
480  * \param old_afsi The audio file selector info before updating.
481  *
482  * The \a old_afsi argument may be \p NULL which indicates that no changes to
483  * the audio file info were made.
484  *
485  * \return Positive on success, negative on errors.
486  */
487 static int mood_update_audio_file(const struct osl_row *aft_row,
488                 struct afs_info *old_afsi)
489 {
490         long score, percent;
491         int ret, is_admissible, was_admissible = 0;
492         struct afs_info afsi;
493         unsigned rank;
494
495         if (!current_mood)
496                 return 1; /* nothing to do */
497         ret = row_belongs_to_score_table(aft_row, &rank);
498         if (ret < 0)
499                 return ret;
500         was_admissible = ret;
501         ret = row_is_admissible(aft_row, current_mood);
502         if (ret < 0)
503                 return ret;
504         is_admissible = (ret > 0);
505         if (!was_admissible && !is_admissible)
506                 return 1;
507         if (was_admissible && !is_admissible)
508                 return delete_from_statistics_and_score_table(aft_row);
509         if (!was_admissible && is_admissible) {
510                 ret = add_afs_statistics(aft_row);
511                 if (ret < 0)
512                         return ret;
513                 return add_to_score_table(aft_row);
514         }
515         /* update score */
516         ret = get_afsi_of_row(aft_row, &afsi);
517         if (ret < 0)
518                 return ret;
519         if (old_afsi) {
520                 ret = update_afs_statistics(old_afsi, &afsi);
521                 if (ret < 0)
522                         return ret;
523         }
524         score = compute_score(&afsi);
525         PARA_DEBUG_LOG("score: %li\n", score);
526         percent = (score + 100) / 3;
527         if (percent > 100)
528                 percent = 100;
529         else if (percent < 0)
530                 percent = 0;
531         PARA_DEBUG_LOG("moving from rank %u to %li%%\n", rank, percent);
532         return score_update(aft_row, percent);
533 }
534
535 /* sse: seconds since epoch. */
536 static void log_statistics(int64_t sse)
537 {
538         unsigned n = statistics.num;
539         int mean_days, sigma_days;
540
541         assert(current_mood);
542         PARA_NOTICE_LOG("loaded mood %s\n", current_mood->name?
543                 current_mood->name : "(dummy)");
544         if (!n) {
545                 PARA_WARNING_LOG("no admissible files\n");
546                 return;
547         }
548         PARA_NOTICE_LOG("%u admissible files\n", statistics.num);
549         mean_days = (sse - statistics.last_played_sum / n) / 3600 / 24;
550         sigma_days = int_sqrt(statistics.last_played_qd / n) / 3600 / 24;
551         PARA_NOTICE_LOG("last_played mean/sigma: %d/%d days\n", mean_days, sigma_days);
552         PARA_NOTICE_LOG("num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n",
553                 statistics.num_played_sum / n,
554                 int_sqrt(statistics.num_played_qd / n));
555         PARA_NOTICE_LOG("num_played correction factor: %" PRId64 "\n",
556                 statistics.num_played_correction);
557         PARA_NOTICE_LOG("last_played correction factor: %" PRId64 "\n",
558                 statistics.last_played_correction);
559         PARA_NOTICE_LOG("normalization divisor: %" PRId64 "\n",
560                 statistics.normalization_divisor);
561 }
562
563 /**
564  * Close the current mood.
565  *
566  * Frees all resources of the current mood.
567  */
568 void close_current_mood(void)
569 {
570         destroy_mood(current_mood);
571         current_mood = NULL;
572         memset(&statistics, 0, sizeof(statistics));
573         statistics.normalization_divisor = 1;
574 }
575
576 static void compute_correction_factors(int64_t sse)
577 {
578         struct afs_statistics *s = &statistics;
579
580         if (s->num > 0) {
581                 s->normalization_divisor = int_sqrt(s->last_played_qd)
582                         * int_sqrt(s->num_played_qd) / s->num / 100;
583                 s->num_played_correction = sse - s->last_played_sum / s->num;
584                 s->last_played_correction = s->num_played_sum / s->num;
585         }
586         if (s->num_played_correction == 0)
587                 s->num_played_correction = 1;
588         if (s->normalization_divisor == 0)
589                 s->normalization_divisor = 1;
590         if (s->last_played_correction == 0)
591                 s->last_played_correction = 1;
592 }
593
594 /**
595  * Change the current mood.
596  *
597  * \param mood_name The name of the mood to open.
598  * \param errmsg Error description is returned here.
599  *
600  * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
601  * and uses a scoring method based only on the \a last_played information.
602  *
603  * The errmsg pointer may be NULL, in which case no error message will be
604  * returned. If a non-NULL pointer is given, the caller must free *errmsg.
605  *
606  * If there is already an open mood, it will be closed first.
607  *
608  * \return Positive on success, negative on errors. Loading the dummy mood
609  * always succeeds.
610  *
611  * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
612  */
613 int change_current_mood(const char *mood_name, char **errmsg)
614 {
615         int i, ret;
616         struct admissible_array aa = {
617                 .size = 0,
618                 .array = NULL
619         };
620         /*
621          * We can not use the "now" pointer from sched.c here because we are
622          * called before schedule(), which initializes "now".
623          */
624         struct timeval rnow;
625
626         if (mood_name) {
627                 struct mood *m;
628                 struct osl_row *row;
629                 struct osl_object obj = {
630                         .data = (char *)mood_name,
631                         .size = strlen(mood_name) + 1
632                 };
633                 ret = osl(osl_get_row(moods_table, BLOBCOL_NAME, &obj, &row));
634                 if (ret < 0) {
635                         if (errmsg)
636                                 *errmsg = make_message("no such mood: %s",
637                                         mood_name);
638                         return ret;
639                 }
640                 ret = load_mood(row, &m, errmsg);
641                 if (ret < 0)
642                         return ret;
643                 close_current_mood();
644                 current_mood = m;
645         } else { /* load dummy mood */
646                 close_current_mood();
647                 current_mood = alloc_new_mood(NULL);
648         }
649         aa.m = current_mood;
650         PARA_NOTICE_LOG("computing statistics of admissible files\n");
651         ret = audio_file_loop(&aa, add_if_admissible);
652         if (ret < 0) {
653                 if (errmsg)
654                         *errmsg = make_message("audio file loop failed");
655                 return ret;
656         }
657         clock_get_realtime(&rnow);
658         compute_correction_factors(rnow.tv_sec);
659         log_statistics(rnow.tv_sec);
660         for (i = 0; i < statistics.num; i++) {
661                 ret = add_to_score_table(aa.array[i]);
662                 if (ret < 0) {
663                         if (errmsg)
664                                 *errmsg = make_message(
665                                         "could not add row to score table");
666                         goto out;
667                 }
668         }
669         ret = statistics.num;
670 out:
671         free(aa.array);
672         return ret;
673 }
674
675 /*
676  * Close and re-open the current mood.
677  *
678  * This function is called on events which render the current list of
679  * admissible files useless, for example if an attribute is removed from the
680  * attribute table.
681  *
682  * If no mood is currently open, the function returns success.
683  */
684 static int reload_current_mood(void)
685 {
686         int ret;
687         char *mood_name = NULL;
688
689         ret = clear_score_table();
690         if (ret < 0)
691                 return ret;
692         if (!current_mood)
693                 return 1;
694         PARA_NOTICE_LOG("reloading %s\n", current_mood->name?
695                 current_mood->name : "(dummy)");
696         if (current_mood->name)
697                 mood_name = para_strdup(current_mood->name);
698         close_current_mood();
699         ret = change_current_mood(mood_name, NULL);
700         free(mood_name);
701         return ret;
702 }
703
704 /**
705  * Notification callback for the moods table.
706  *
707  * \param event Type of the event just occurred.
708  * \param pb Unused.
709  * \param data Its type depends on the event.
710  *
711  * This function performs actions required due to the occurrence of the given
712  * event. Possible actions include reload of the current mood and update of the
713  * score of an audio file.
714  *
715  * \return Standard.
716  */
717 int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb,
718                 void *data)
719 {
720         if (!current_mood)
721                 return 0;
722         switch (event) {
723         /*
724          * The three blob events might change the set of admissible files,
725          * so we must reload the score list.
726          */
727         case BLOB_RENAME:
728         case BLOB_REMOVE:
729         case BLOB_ADD:
730                 if (data == moods_table || data == playlists_table)
731                         return 1; /* no reload necessary for these */
732                 return reload_current_mood();
733         /* these also require reload of the score table */
734         case ATTRIBUTE_ADD:
735         case ATTRIBUTE_REMOVE:
736         case ATTRIBUTE_RENAME:
737                 return reload_current_mood();
738         /* changes to the aft only require to re-examine the audio file */
739         case AFSI_CHANGE: {
740                 struct afsi_change_event_data *aced = data;
741                 return mood_update_audio_file(aced->aft_row, aced->old_afsi);
742                 }
743         case AFHI_CHANGE:
744         case AUDIO_FILE_RENAME:
745         case AUDIO_FILE_ADD:
746                 return mood_update_audio_file(data, NULL);
747         case AUDIO_FILE_REMOVE:
748                 return mood_delete_audio_file(data);
749         default:
750                 return 1;
751         }
752 }