1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 
7 #include <glib.h>
8 #include <glib/gi18n.h>
9 #include "interface_common.h"
10 #include "main.h"
11 #include "config_window_handler.h"
12 #include "players_manipulation.h"
13 #include "ripper_encoder_manipulation.h"
14 #include "select_frame_handler.h"
15 #include "job_control.h"
16 #include "status_frame_handler.h"
17 #include "cddb.h"
18 #include "misc_utils.h"
19 #include "version.h"
20 
21 #include "main_window_handler.h"
22 
23 /* Xpms */
24 #include "xpms/ripperX.xpm"
25 #include "xpms/config.xpm"
26 #include "xpms/scan.xpm"
27 #include "xpms/go.xpm"
28 #include "xpms/stop.xpm"
29 #include "xpms/cddb.xpm"
30 #include "xpms/exit.xpm"
31 
32 
33 void mw_config_button_clicked( GtkWidget *widget, gpointer callback_data );
34 void mw_stop_button_clicked( GtkWidget *widget, gpointer callback_data );
35 void mw_go_button_clicked( GtkWidget *widget, gpointer callback_data );
36 void mw_pause_button_clicked( GtkWidget *widget, gpointer callback_data );
37 void mw_exit_button_clicked( GtkWidget *widget, gpointer callback_data );
38 static int check_dirs();
39 
40 
41 void mw_config_button_clicked( GtkWidget *widget, gpointer callback_data )
42 {
43 	main_window_handler( MW_MODE_CONFIG, 0, NULL );
44 	config_window_handler( WIDGET_CREATE, ( _main_data * ) callback_data );
45 	return;
46 }
47 
48 void mw_scan_button_clicked( GtkWidget *widget, gpointer callback_data )
49 {
50 	_main_data * main_data;
51 
52 	main_data = ( _main_data * ) callback_data;
53 	memset( main_data, 0, sizeof( _main_data ) );
54 
55 	select_frame_handler( WIDGET_DESTROY, 0, main_data );
56 	select_frame_handler( CLEAR_ENTRIES, 0, main_data );
57 
58 	/* check to make sure there is a cd in the drive.. if not dim out
59 	   cddb and go buttons to prevent errors from even happening....
60 	   otherwise make sure they can use the buttons */
61 
62 	if ( !scan_cd( main_data ) )
63 		main_window_handler(CD_INDRIVE, NULL, NULL);
64 	else
65 		main_window_handler(NO_CD_INDRIVE, NULL, NULL);
66 
67 	select_frame_handler( WIDGET_CREATE, 0, main_data );
68 	select_frame_handler( SF_SYNC_SELECT_FRAME, 0, main_data );
69 	return;
70 }
71 
72 void mw_go_button_clicked( GtkWidget *widget, gpointer callback_data )
73 {
74     if (!check_dirs()) {
75         return;
76     }
77 	play_cd_wav_mp3( STOP, CD, NULL );
78 	play_cd_wav_mp3( STOP, WAV, NULL );
79 	play_cd_wav_mp3( STOP, MP3, NULL );
80 	job_starter( ( _main_data * ) callback_data );
81 	return;
82 }
83 
84 void mw_stop_button_clicked( GtkWidget *widget, gpointer callback_data )
85 {
86 	play_cd_wav_mp3( STOP, CD, NULL );
87 	play_cd_wav_mp3( STOP, WAV, NULL );
88 	play_cd_wav_mp3( STOP, MP3, NULL );
89 	return;
90 }
91 
92 void mw_cddb_button_clicked( GtkWidget *widget, gpointer callback_data )
93 {
94 	_main_data * main_data;
95 
96 	main_data = ( _main_data * ) callback_data;
97 	cddb_main( ( _main_data* ) callback_data );
98 	return;
99 }
100 
101 void mw_exit_button_clicked( GtkWidget *widget, gpointer callback_data )
102 {
103 	int mode;
104 
105 	mode = *( int * ) callback_data;
106 
107 	ripperX_exit( NULL, NULL );
108 	return;
109 }
110 
111 GtkWidget *main_window_handler( int ops, char *statusbar_msg,
112                                 _main_data *main_data )
113 {
114 	static GtkWidget * main_frame, *statusbar;
115 	static int saved_mode;
116 	static int count = 0;
117 
118 	static struct {
119 		GtkWidget *button;
120 		char **xpm;
121 		void *func;
122 		gpointer callback_data;
123 		int arrangement;
124 		/* If arrangement is TRUE, it will be packed using gtk_pack_start
125 		 * function. Otherwise, it will use gtk_pack_end function */
126 		char *tooltip;
127 	} buttons[] =
128 	    {
129 	        { NULL, config_xpm, mw_config_button_clicked, NULL,
130 	          TRUE, N_("Configuration") },
131 	        { NULL, scan_xpm, mw_scan_button_clicked, NULL,
132 	          TRUE, N_("Scan CD") },
133 	        { NULL, stop_xpm, mw_stop_button_clicked, NULL,
134 	          TRUE, N_("Stop playing") },
135 	        { NULL, cddb_xpm, mw_cddb_button_clicked, NULL,
136 	          TRUE, N_("Get track titles from CDDB server") },
137 	        { NULL, go_xpm, mw_go_button_clicked, NULL,
138 	          TRUE, N_("Start ripping&encoding") },
139 	        { NULL, exit_xpm, mw_exit_button_clicked, NULL,
140 	          FALSE, N_("Exit the program") },
141 	    };
142 
143 	buttons[ 0 ].callback_data = ( gpointer ) main_data;
144 	buttons[ 1 ].callback_data = ( gpointer ) main_data;
145 	buttons[ 3 ].callback_data = ( gpointer ) main_data;
146 	buttons[ 4 ].callback_data = ( gpointer ) main_data;
147 	buttons[ 5 ].callback_data = ( gpointer ) & saved_mode;
148 
149 	switch ( ops ) {
150 	case WIDGET_CREATE : {
151 			/* Variables used only when creating widget */
152 			GtkWidget * main_window, *bbox, *vbox;
153 			GtkTooltips *tooltips;
154 			GtkWidget *pixmap;
155 			GdkPixmap *gdk_pixmap;
156 			GdkBitmap *mask;
157 			GtkStyle *style;
158 			GdkGC *gc;
159 			GdkColormap *colormap;
160 			static GdkColor tooltips_bg;
161 			int i, num_buttons;
162 			char welcome_msg_buf[ 100 ];
163 
164 			num_buttons = sizeof( buttons ) / sizeof( buttons[ 0 ] );
165 
166 			main_window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
167 			gtk_widget_set_name( main_window, "main window" );
168 			gtk_widget_set_size_request( main_window, 508, 406 );
169 			gtk_widget_realize( main_window );
170 
171 			tooltips = gtk_tooltips_new();
172 
173 			colormap = gdk_window_get_colormap( main_window->window );
174 			tooltips_bg.red = 61669;
175 			tooltips_bg.green = 59113;
176 			tooltips_bg.blue = 35979;
177 			gdk_color_alloc( colormap, &tooltips_bg );
178 
179 /*			gtk_tooltips_set_colors ( tooltips,
180 			                          &tooltips_bg,
181 			                          &main_window->style->fg[ GTK_STATE_NORMAL ] );*/
182 			g_object_set_data( G_OBJECT( main_window ), "tooltips", tooltips );
183 
184 			g_signal_connect( G_OBJECT( main_window ), "destroy",
185 			                    G_CALLBACK( ripperX_exit ),
186 			                    NULL );
187 			g_signal_connect( G_OBJECT( main_window ), "delete_event",
188 			                    G_CALLBACK( ripperX_exit ),
189 			                    NULL );
190 
191 			/* Create a vbox to contain things */
192 			vbox = gtk_vbox_new( FALSE, 0 );
193 			gtk_container_add( GTK_CONTAINER( main_window ), vbox );
194 			gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
195 			gtk_widget_realize( vbox );
196 			gtk_widget_show( vbox );
197 
198 			/* Create button box & buttons */
199 			bbox = gtk_hbox_new( FALSE, 0 );
200 			gtk_box_pack_start( GTK_BOX( vbox ), bbox, FALSE, FALSE, 0 );
201 			gtk_container_set_border_width( GTK_CONTAINER( bbox ), 0 );
202 
203 			gtk_widget_realize( bbox );
204 			gtk_widget_show( bbox );
205 
206 			/* logo */
207 			style = gtk_widget_get_default_style();
208 			gc = style->black_gc;
209 			gdk_pixmap = gdk_pixmap_create_from_xpm_d( bbox->window, &mask,
210 			             &style->bg[ GTK_STATE_NORMAL ],
211 			             ripperX_xpm );
212 			pixmap = gtk_pixmap_new( gdk_pixmap, mask );
213 			gtk_box_pack_start( GTK_BOX( bbox ), pixmap, FALSE, FALSE, 0 );
214 
215 			for ( i = 0; i < num_buttons; i++ ) {
216 				/* Create pixmap */
217 				style = gtk_widget_get_default_style();
218 				gc = style->black_gc;
219 				gdk_pixmap = gdk_pixmap_create_from_xpm_d( bbox->window, &mask,
220 				             &style->bg[ GTK_STATE_NORMAL ],
221 				             buttons[ i ].xpm );
222 				pixmap = gtk_pixmap_new( gdk_pixmap, mask );
223 
224 				/* Pack pixmap into button */
225 				buttons[ i ].button = gtk_button_new();
226 				gtk_container_add( GTK_CONTAINER( buttons[ i ].button ), pixmap );
227 
228 				g_signal_connect( G_OBJECT( buttons[ i ].button ), "clicked",
229 				                    G_CALLBACK( buttons[ i ].func ),
230 				                    ( gpointer ) buttons[ i ].callback_data );
231 				if ( buttons[ i ].arrangement == TRUE )
232 					gtk_box_pack_start( GTK_BOX( bbox ), buttons[ i ].button, FALSE, FALSE, 0 );
233 				else
234 					gtk_box_pack_end( GTK_BOX( bbox ), buttons[ i ].button, FALSE, FALSE, 0 );
235 				gtk_tooltips_set_tip( tooltips, buttons[ i ].button,
236 				                      buttons[ i ].tooltip, NULL );
237 			}
238 
239 			/* Create main frame */
240 			main_frame = gtk_frame_new( "ripperX" );
241 			gtk_box_pack_start( GTK_BOX( vbox ), main_frame, TRUE, TRUE, 0 );
242 
243 			statusbar = gtk_statusbar_new();
244 			gtk_box_pack_end( GTK_BOX( vbox ), statusbar, FALSE, FALSE, 0 );
245 
246 			/* Push welcome message */
247 			sprintf( welcome_msg_buf, " RipperX, v%s", VERSION );
248 			gtk_statusbar_push( GTK_STATUSBAR( statusbar ), 1, welcome_msg_buf );
249 
250 			/* Read main_data & Create select window here */
251 
252 			count = scan_cd( main_data );
253 
254 			select_frame_handler( WIDGET_CREATE, 0, main_data );
255 			select_frame_handler( SF_SYNC_SELECT_FRAME, 0, main_data );
256 			gtk_widget_show_all( main_window );
257 
258 			/* Do a CDDB lookup for this CD only if cd is actually is drive */
259 			if ( config.cddb_config.auto_lookup && !count )
260 				cddb_main( main_data );
261 
262 		}
263 		/* go on and set mode to select mode */
264 
265 	case MW_MODE_SELECT :
266 		saved_mode = ops;
267 		/* where now is used by ripperX_exit */
268 		where_now = SELECT_FRAME;
269 		gtk_widget_set_sensitive( buttons[ 0 ].button, TRUE );
270 		gtk_widget_set_sensitive( buttons[ 1 ].button, TRUE );
271 		gtk_widget_set_sensitive( buttons[ 2 ].button, TRUE );
272 		/* disable cddb and go buttons since no cd anyway... prevent
273 		   stupid errors*/
274 		if (count)
275 		{
276 			gtk_widget_set_sensitive( buttons[ 3 ].button, FALSE );
277 			gtk_widget_set_sensitive( buttons[ 4 ].button, FALSE );
278 		}
279 		else
280 		{
281 			gtk_widget_set_sensitive( buttons[ 3 ].button, TRUE );
282 			gtk_widget_set_sensitive( buttons[ 4 ].button, TRUE );
283 		}
284 		gtk_widget_set_sensitive( buttons[ 5 ].button, TRUE );
285 
286 		return main_frame;
287 
288 	case MW_MODE_STATUS :
289 		saved_mode = ops;
290 		/* where now is used by ripperX_exit */
291 		where_now = STATUS_FRAME;
292 		gtk_widget_set_sensitive( buttons[ 0 ].button, FALSE );
293 		gtk_widget_set_sensitive( buttons[ 1 ].button, FALSE );
294 		gtk_widget_set_sensitive( buttons[ 2 ].button, FALSE );
295 		gtk_widget_set_sensitive( buttons[ 3 ].button, FALSE );
296 		gtk_widget_set_sensitive( buttons[ 4 ].button, FALSE );
297 		gtk_widget_set_sensitive( buttons[ 5 ].button, TRUE );
298 
299 		return main_frame;
300 
301 	case MW_MODE_CONFIG :
302 		saved_mode = ops;
303 		/* where now is used by ripperX_exit */
304 		where_now = CONFIG_WINDOW;
305 		gtk_widget_set_sensitive( buttons[ 0 ].button, FALSE );
306 		gtk_widget_set_sensitive( buttons[ 1 ].button, FALSE );
307 		gtk_widget_set_sensitive( buttons[ 2 ].button, FALSE );
308 		gtk_widget_set_sensitive( buttons[ 3 ].button, FALSE );
309 		gtk_widget_set_sensitive( buttons[ 4 ].button, FALSE );
310 		gtk_widget_set_sensitive( buttons[ 5 ].button, TRUE );
311 
312 		return main_frame;
313 
314 	case MW_CLEAR_STATUSBAR :
315 		while ( --count >= 0 )
316 			gtk_statusbar_pop( GTK_STATUSBAR( statusbar ), 1 );
317 		count++;
318 		return main_frame;
319 
320 	case MW_UPDATE_STATUSBAR :
321 		count++;
322 		gtk_statusbar_push( GTK_STATUSBAR( statusbar ), 1,
323 		                    statusbar_msg );
324 		return main_frame;
325 
326 	case MW_REQUEST_MF :
327 		return main_frame;
328 
329 	case NO_CD_INDRIVE:
330 		gtk_widget_set_sensitive( buttons[ 3 ].button, FALSE );
331 		gtk_widget_set_sensitive( buttons[ 4 ].button, FALSE );
332 		return main_frame;
333 
334  	case CD_INDRIVE:
335 		gtk_widget_set_sensitive( buttons[ 3 ].button, TRUE );
336 		gtk_widget_set_sensitive( buttons[ 4 ].button, TRUE );
337 		return main_frame;
338 
339 	}
340 	return main_frame;
341 }
342 
343 static int check_dirs() {
344     long wav_free, mp3_free;
345     int rc;
346 
347     rc = check_dir(config.wav_path);
348     switch (rc) {
349         case MISC_DOES_NOT_EXISTS:
350             rc = dialog_handler(WIDGET_CREATE, FALSE, 7, 0, NULL, NULL, 0);
351             if (!rc) {
352                 return 0;
353             } else {
354                 if (create_dir(config.wav_path) != 0) {
355                     err_handler(29, NULL);
356                     return 0;
357                 }
358             }
359             break;
360         case MISC_NOT_DIR:
361         case MISC_NOT_WRITABLE:
362             err_handler(27, NULL);
363             return 0;
364             break;
365     }
366 
367     rc = check_dir(config.mp3_path);
368     switch (rc) {
369         case MISC_DOES_NOT_EXISTS:
370             rc = dialog_handler(WIDGET_CREATE, FALSE, 8, 0, NULL, NULL, 0);
371             if (!rc) {
372                 return 0;
373             } else {
374                 if (create_dir(config.mp3_path) != 0) {
375                     err_handler(30, NULL);
376                     return 0;
377                 }
378             }
379             break;
380         case MISC_NOT_DIR:
381         case MISC_NOT_WRITABLE:
382             err_handler(28, NULL);
383             return 0;
384             break;
385     }
386 
387     wav_free = check_free_space(config.wav_path);
388     mp3_free = check_free_space(config.mp3_path);
389 
390     if (wav_free < 500 * 1024) {
391         /* warn if less than 500 MB free on wav partition */
392         rc = dialog_handler(WIDGET_CREATE, FALSE, 5, 0, NULL, NULL, 0);
393         if (!rc) {
394             return 0;
395         }
396     }
397     if (wav_free != mp3_free && mp3_free < 100 * 1024) {
398         /* only warn if on different partition and less
399          * than 100 MB free */
400         rc = dialog_handler(WIDGET_CREATE, FALSE, 6, 0, NULL, NULL, 0);
401         if (!rc) {
402             return 0;
403         }
404     }
405 
406     /* good to go */
407     return 1;
408 
409 }
410 
411