1 /*
2  * handlers_playqueue.c
3  * Copyright 2005-2013 George Averill, Ariadne Conill, Yoshiki Yazawa,
4  *                     Matti Hämäläinen, and John Lindgren
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions, and the following disclaimer in the documentation
14  *    provided with the distribution.
15  *
16  * This software is provided "as is" and without any warranty, express or
17  * implied. In no event shall the authors be liable for any damages arising from
18  * the use of this software.
19  */
20 
21 #include <stdlib.h>
22 
23 #include "audtool.h"
24 #include "wrappers.h"
25 
playqueue_add(int argc,char ** argv)26 void playqueue_add (int argc, char * * argv)
27 {
28     int pos = check_args_playlist_pos (argc, argv);
29     obj_audacious_call_playqueue_add_sync (dbus_proxy, pos - 1, NULL, NULL);
30 }
31 
playqueue_remove(int argc,char ** argv)32 void playqueue_remove (int argc, char * * argv)
33 {
34     int pos = check_args_playlist_pos (argc, argv);
35     obj_audacious_call_playqueue_remove_sync (dbus_proxy, pos - 1, NULL, NULL);
36 }
37 
playqueue_is_queued(int argc,char ** argv)38 void playqueue_is_queued (int argc, char * * argv)
39 {
40     int pos = check_args_playlist_pos (argc, argv);
41     find_in_queue (pos - 1); /* calls exit(1) if not found */
42 
43     exit (0);
44 }
45 
playqueue_get_queue_position(int argc,char ** argv)46 void playqueue_get_queue_position (int argc, char * * argv)
47 {
48     int pos = check_args_playlist_pos (argc, argv);
49     audtool_report ("%d", find_in_queue (pos - 1) + 1);
50 }
51 
playqueue_get_list_position(int argc,char ** argv)52 void playqueue_get_list_position (int argc, char * * argv)
53 {
54     int qpos = check_args_playlist_pos (argc, argv);
55     audtool_report ("%d", get_queue_entry (qpos - 1) + 1);
56 }
57 
playqueue_display(int argc,char ** argv)58 void playqueue_display (int argc, char * * argv)
59 {
60     int qlength = get_queue_length ();
61 
62     audtool_report ("%d queued tracks.", qlength);
63 
64     int total = 0;
65 
66     for (int qpos = 0; qpos < qlength; qpos ++)
67     {
68         int pos = get_queue_entry (qpos);
69         char * title = get_entry_title (pos);
70         int length = get_entry_length (pos) / 1000;
71 
72         total += length;
73 
74         /* adjust width for multi byte characters */
75         int column = 60;
76 
77         for (const char * p = title; * p; p = g_utf8_next_char (p))
78         {
79             int stride = g_utf8_next_char (p) - p;
80 
81             if (g_unichar_iswide (g_utf8_get_char (p)) ||
82              g_unichar_iswide_cjk (g_utf8_get_char (p)))
83                 column += (stride - 2);
84             else
85                 column += (stride - 1);
86         }
87 
88         char * fmt = g_strdup_printf ("%%4d | %%4d | %%-%ds | %%d:%%.2d", column);
89         audtool_report (fmt, qpos + 1, pos + 1, title, length / 60, length % 60);
90         g_free (fmt);
91     }
92 
93     audtool_report ("Total length: %d:%.2d", total / 60, total % 60);
94 }
95 
playqueue_length(int argc,char ** argv)96 void playqueue_length (int argc, char * * argv)
97 {
98     audtool_report ("%d", get_queue_length ());
99 }
100 
playqueue_clear(int argc,char ** argv)101 void playqueue_clear (int argc, char * * argv)
102 {
103     obj_audacious_call_playqueue_clear_sync (dbus_proxy, NULL, NULL);
104 }
105