#!/usr/bin/ruby # cover titler, application pour scanner des cd et insérer les titres des différents fichiers médias trouvés dans un fichier svg pour faire des couvertures $version = "0.3" #-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #initialisation require 'gtk2' if File.directory?( '/media' ) == true $chemin_cd = "/media/cdrom" else $chemin_cd = "/mnt/cdrom" end $home = `echo $HOME`.chomp Dir.mkdir( $home + '/.covertitler' ) if File.directory?( $home + '/.covertitler' ) == false Dir.mkdir( $home + '/.covertitler/models' ) if File.directory?( $home + '/.covertitler/models' ) == false $fichiers = Array.new( 2 ) $fichiers[ 0 ], $fichiers[ 1 ] = $home + "/.covertitler/models/templates_divx.svg", $home + "/.covertitler/new_cover.svg" $positions = Array.new( 2 ) $positions[ 0 ] = [ 1433, 1437, 1441, 1445, 1449, 1453 ] $positions[ 1 ] = [ 2340, 2344, 2348, 2352, 2356, 2360 ] $titres = Array.new( 2, Array.new( 6 ) ) $scan_done = false #-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #méthodes, classes, fonctions #----------------------------------------------------------------------------------------------------------------------- def fenetre_scan Gtk.init window_scan = Gtk::Window.new window_scan.set_title( 'choix du scan' ) window_scan.border_width = 25 window_scan.signal_connect( 'delete_event' ) { window_scan.destroy; Gtk.main_quit } main_box_scan = Gtk::VBox.new( true, 20) window_scan.add( main_box_scan ) lbl_en_tete_scan = Gtk::Label.new( 'qui pour qui quoi?' ) main_box_scan.pack_start( lbl_en_tete_scan, false, false, 0 ) table_scan = Gtk::Table.new( 2, 2, false ) table_scan.set_row_spacings( 10 ) table_scan.set_column_spacings( 5 ) main_box_scan.pack_start( table_scan, false, false, 0 ) lbl_lecteur = Gtk::Label.new( 'lecteur' ) table_scan.attach_defaults( lbl_lecteur, 0, 1, 0, 1 ) combobox_lecteur = Gtk::ComboBox.new table_scan.attach_defaults( combobox_lecteur, 1, 2, 0, 1 ) combobox_lecteur.append_text( ' cdrom 1 ' ) combobox_lecteur.append_text( ' cdrom 2 ' ) combobox_lecteur.set_active( 0 ) lbl_couv = Gtk::Label.new( 'couverture' ) table_scan.attach_defaults( lbl_couv, 0, 1, 1, 2 ) combobox_couv = Gtk::ComboBox.new table_scan.attach_defaults( combobox_couv, 1, 2, 1, 2 ) items_couv = Array.new combobox_couv.append_text( ' couverture 1 ' ) combobox_couv.append_text( ' couverture 2 ' ) combobox_couv.set_active( 0 ) hbox_boutons = Gtk::HBox.new( true, 5 ) main_box_scan.pack_start( hbox_boutons, false, false, 0 ) bouton_annuler_scan = Gtk::Button.new( "annuler" ) hbox_boutons.pack_start( bouton_annuler_scan, false, true, 0 ) bouton_annuler_scan.signal_connect( 'clicked' ) { window_scan.destroy; Gtk.main_quit } bouton_ok_scan = Gtk::Button.new( "scanner" ) hbox_boutons.pack_start( bouton_ok_scan, false, true, 0 ) bouton_ok_scan.signal_connect( 'clicked' ) { window_scan.set_sensitive( false ) scanner( combobox_lecteur.active, combobox_couv.active ) window_scan.destroy Gtk.main_quit } window_scan.show_all Gtk.main end #----------------------------------------------------------------------------------------------------------------------- def scanner( lecteur, couverture ) dev = 'hdc'; dev = 'hdd' if lecteur == 1 `eject /dev/#{dev}` Gtk.init window_scanner = Gtk::Window.new window_scanner.set_title( 'inserer cd' ) window_scanner.border_width = 25 window_scanner.signal_connect( 'delete_event' ) { window_scanner.destroy; Gtk.main_quit } main_box_scanner = Gtk::VBox.new( true, 10 ) window_scanner.add( main_box_scanner ) lbl_scanner = Gtk::Label.new( "veuiller inserer\nvotre cd" ) main_box_scanner.pack_start( lbl_scanner, false, false, 0 ) bouton_scanner = Gtk::Button.new( 'ok' ) main_box_scanner.pack_start( bouton_scanner, false, true, 0 ) bouton_scanner.signal_connect( 'clicked' ) { window_scanner.destroy; Gtk.main_quit } window_scanner.show_all Gtk.main `eject -t /dev/#{dev}` #vérification de la présence des divix nombre_divix, titres = 0, Array.new Dir.foreach( $chemin_cd ) { |fichier| fichier = fichier.downcase if fichier.match( 'avi' ) != nil titres[ nombre_divix ] = fichier nombre_divix += 1 end } if nombre_divix != 6 puts "Il n'y a pas 6 et/ou juste 6 divix sur ce cd" exit end $titres[ couverture ] = titres.sort end #----------------------------------------------------------------------------------------------------------------------- def fenetre_fichiers( id ) Gtk.init window_fichier = Gtk::Window.new window_fichier.set_title( 'choix du fichier' ) window_fichier.border_width = 25 window_fichier.signal_connect( 'delete_event' ) { window_fichier.destroy; Gtk.main_quit } main_box_fichier = Gtk::VBox.new( false, 10 ) window_fichier.add( main_box_fichier ) chooser_fichier = Gtk::FileChooserWidget.new( Gtk::FileChooser::ACTION_SAVE, nil ) main_box_fichier.pack_start( chooser_fichier, false,false, 0 ) chooser_fichier.set_current_folder( $home + "/.covertitler/models/" ); chooser_fichier.set_current_folder( $home + "/.covertitler" ) if id == 1 chooser_fichier.set_filename( $fichiers[ id ] ) bouton_fichier = Gtk::Button.new( 'choisir' ) main_box_fichier.pack_start( bouton_fichier, false, false, 0 ) bouton_fichier.signal_connect( 'clicked' ) { nom = chooser_fichier.filename nom += '.svg' if nom.match( 'svg' ) == nil $fichiers[ id ] = nom window_fichier.destroy Gtk.main_quit } window_fichier.show_all Gtk.main end #----------------------------------------------------------------------------------------------------------------------- #fenetre principale def fenetre_principale Gtk.init main_window = Gtk::Window.new main_window.border_width= 25 main_window.set_title( 'cover titler ' + $version ) main_box = Gtk::VBox.new( false, 10 ) main_window.add( main_box ) en_tete = Gtk::Label.new( "les titres seront les suivants" ) main_box.pack_start( en_tete, false, false, 0 ) #labels couverture hbox_couv = Gtk::HBox.new( true, 0 ) main_box.pack_start( hbox_couv, false, true, 0 ) 2.times { |i| lbl_couv = Gtk::Label.new( "couverture #{i+1}" ) hbox_couv.pack_start( lbl_couv, false, false, 0 ) } # table des titres table_titres = Gtk::Table.new( 6, 5, false ) table_titres.set_row_spacings( 5 ) table_titres.set_column_spacings( 5 ) main_box.pack_start( table_titres, false,false, 0 ) @entry_titres = Array.new 12.times { |i| num_titre = 'titre ' + ( i + 1 ).to_s num_titre = 'titre ' + ( i - 5 ).to_s if i >= 6 label = Gtk::Label.new( num_titre ) hsup = 0; hsup = 3 if i >= 6 vsup = 0; vsup = 6 if i >= 6 table_titres.attach_defaults( label, 0 + hsup, 1 + hsup, i - vsup, (i+1) - vsup ) @entry_titres[ i ] = Gtk::Entry.new @entry_titres[ i ].set_editable( true ) titres = $titres[ 0 ]; titres = $titres[ 1 ] if i >= 6 @entry_titres[ i ].set_text( titres[ i - vsup ] ) table_titres.attach( @entry_titres[ i ], 1 + hsup, 2 + hsup, i - vsup, (i + 1) - vsup, xopt = Gtk::EXPAND, yopt = Gtk::EXPAND, 10, 0 ) } vsep = Gtk::VSeparator.new table_titres.attach_defaults( vsep, 2, 3, 0, 6 ) hsep = Gtk::HSeparator.new main_box.pack_start( hsep, false, false, 0 ) # table de selection des fichier table_fichiers = Gtk::Table.new( 2, 3, false ) table_fichiers.set_row_spacings( 10 ) table_fichiers.set_column_spacings( 6 ) main_box.pack_start( table_fichiers, false,false, 0 ) @entry_fichiers = Array.new 2.times { |i| titre = "modele"; titre = "cible" if i == 1 label = Gtk::Label.new( titre ) table_fichiers.attach_defaults( label, 0, 1, i, i + 1) @entry_fichiers[ i ] = Gtk::Entry.new @entry_fichiers[ i ].set_editable(true ) @entry_fichiers[ i ].set_text( $fichiers[ i ] ) table_fichiers.attach_defaults( @entry_fichiers[ i ], 1, 2, i, i+1 ) bouton = Gtk::Button.new( '...' ) bouton.signal_connect( 'clicked' ) { main_window.set_sensitive( false ) fenetre_fichiers( i ) main_window.destroy Gtk.main_quit } table_fichiers.attach_defaults( bouton, 2, 3, i, i+1 ) } #hbox des boutons du bas hbox_boutons = Gtk::HBox.new( true, 10 ) main_box.pack_start( hbox_boutons, false, false, 0 ) bouton_scanner = Gtk::Button.new( 'scanner' ) bouton_scanner.signal_connect( 'clicked' ) { main_window.set_sensitive( false ) fenetre_scan main_window.destroy Gtk.main_quit } hbox_boutons.pack_start( bouton_scanner, false, true, 0 ) bouton_annuler = Gtk::Button.new( 'annuler' ) bouton_annuler.signal_connect( 'clicked' ) { exit } hbox_boutons.pack_start( bouton_annuler, false, true, 0 ) bouton_ok = Gtk::Button.new( 'ok' ) bouton_ok.signal_connect( 'clicked' ) { $scan_done = true 2.times { |couv| valid_titres = $titres[ couv ] valid_titres.each { |nom| $scan_done = false if nom == nil } } fenetre_erreur( 'titre_manquant' ) if $scan_done == false main_window.destroy Gtk.main_quit } hbox_boutons.pack_start( bouton_ok, false, true, 0 ) main_window.show_all Gtk.main end #----------------------------------------------------------------------------------------------------------------------- # fenetre des erreurs def fenetre_erreur( erreur ) Gtk.init window_erreur = Gtk::Window.new window_erreur.border_width = 25 window_erreur.set_title( 'erreur' ) window_erreur.signal_connect( 'delete_event' ) { window_erreur.destroy; Gtk.main_quit } main_box_erreur = Gtk::VBox.new( false, 10 ) window_erreur.add( main_box_erreur ) entete_erreur = Gtk::Label.new( 'erreur!' ) main_box_erreur.pack_start( entete_erreur, false, false, 0 ) case erreur when 'titre_manquant' : message_erreur = "Il semble que tous les titres\nn'aient pas ete attribues" end lbl_message = Gtk::Label.new( message_erreur ) main_box_erreur.pack_start( lbl_message, false, false, 0 ) bouton_erreur = Gtk::Button.new( 'ok' ) main_box_erreur.pack_start( bouton_erreur, false, true, 0 ) bouton_erreur.signal_connect( 'clicked' ) { window_erreur.destroy; Gtk.main_quit } window_erreur.show_all Gtk.main puts 'en construction' end #------------------------------------------------------------------------------------------------------------- #on propose à l'utilisateur de visionner le résultat def finir Gtk.init window = Gtk::Window.new window.border_width = 25 window.set_title( "c'est tout bon" ) window.signal_connect( 'delete_event' ) { exit } vbox1 = Gtk::VBox.new( false, 18 ) window.add( vbox1 ) text = Gtk::Label.new( "Felicitation\n\nLes changements ont ete effectue avec succes.\n\nVoulez-vous voir le resultat avec inkscape\nou fermer l'application?" ) vbox1.pack_start( text, false, false, 0 ) hbox1 = Gtk::HBox.new( true, 8 ) vbox1.pack_start( hbox1, false, false, 0 ) bouton1 = Gtk::Button.new( "lancer inkscape" ) bouton1.signal_connect( 'clicked' ) { window.destroy; Gtk.main_quit } hbox1.pack_end( bouton1, true, true, 0 ) bouton2 = Gtk::Button.new( "terminer" ) bouton2.signal_connect( 'clicked' ) { exit } hbox1.pack_end( bouton2, true, true, 0 ) window.show_all Gtk.main end #-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # séquence principale #----------------------------------------------------------------------------------------------------------------------- #GUI while $scan_done == false fenetre_principale end #----------------------------------------------------------------------------------------------------------------------- #récupération du code du fichier modèle $lignes = IO.readlines( $fichiers[ 0 ] ) #----------------------------------------------------------------------------------------------------------------------- #modification des lignes 2.times { |couv| position_lignes = $positions[ couv ] titres = $titres[ couv ] 6.times { |i| coord_y = $lignes[ position_lignes[ i ] ].split( '>' ) $lignes[ position_lignes[ i ] ] = coord_y[ 0 ] + ">" + titres[ i ] + "" + titres[ i ] + "" if i == 5 && couv == 0 } } puts 'done' #----------------------------------------------------------------------------------------------------------------------- #on écrit les titres dans le fichier .svg fichier_nouveau = File.new( $fichiers[ 1 ], "w" ) fichier_nouveau.puts( $lignes ) fichier_nouveau.close #----------------------------------------------------------------------------------------------------------------------- #finalisation finir `inkscape #{$fichiers[ 1 ]}`