rendered paste body#!/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.4"#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#initialisationrequire 'gtk2'if File.directory?( '/media' ) == true $chemin_cd = "/media/cdrom"else $chemin_cd = "/mnt/cdrom" end$home = `echo $HOME`.chompDir.mkdir( $home + '/.covertitler' ) if File.directory?( $home + '/.covertitler' ) == falseDir.mkdir( $home + '/.covertitler/models' ) if File.directory?( $home + '/.covertitler/models' ) == false$entry_titres = Array.new( 2 )$couvertures = Array.new( 2 )#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#méthodes, classes, fonctions#-----------------------------------------------------------------------------------------------------------------------class Couverture def initialize( numero, nbr_titres ) @nom = 'couverture' + ( numero + 1 ).to_s @titres = Array.new( nbr_titres, " " ) @positions = [ 1433, 1437, 1441, 1445, 1449, 1453 ] @positions = [ 2340, 2344, 2348, 2352, 2356, 2360 ] if numero == 1 end def nom @nom end def titres @titres end def titre( i ) @titres[ i ] end def set_titre( num, nom ) @titres[ num ] = nom end def position( i ) @positions[ i ] end def titres_sort @titres = @titres.sort end end#-----------------------------------------------------------------------------------------------------------------------class Configuration def initialize @scan_done = false @nombre_titres = 6 @fichiers = Array.new( 2 ) @fichiers[ 0 ], @fichiers[ 1 ] = $home + "/.covertitler/models/templates_divx.svg", $home + "/.covertitler/new_cover.svg" end def scan? @scan_done end def scan_ok @scan_done = true end def nbr_titres? @nombre_titres end def set_nbr_titres( nouveau_nombre ) @nombre_titres = nouveau_nombre end def fichiers?( i ) @fichiers[ i ] end def set_fichier( i, nouveau_chemin ) @fichier[ i ] = nouveau_chemin end end#-----------------------------------------------------------------------------------------------------------------------#fenetre principaledef 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( $couvertures[ i ].nom ) 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 ) 2.times { |couv| $entry_titres[ couv ] = Array.new entry = Array.new i = 0; tableau = $couvertures[ couv ].titres tableau.each { |titre| label = Gtk::Label.new( 'titre ' + ( i + 1 ).to_s ) sup = 0; sup = 3 if couv == 1 table_titres.attach_defaults( label, 0 + sup, 1 + sup, i, (i+1) ) entry[ i ] = Gtk::Entry.new entry[ i ].set_editable( true ) entry[ i ].set_text( $couvertures[ couv ].titre( i ) ) table_titres.attach( entry[ i ], 1 + sup, 2 + sup, i, i + 1, xopt = Gtk::EXPAND, yopt = Gtk::EXPAND, 10, 0 ) i += 1 } $entry_titres[ couv ] = entry } 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( $config.fichiers?( i ) ) table_fichiers.attach_defaults( $entry_fichiers[ i ], 1, 2, i, i+1 ) bouton = Gtk::Button.new( '...' ) bouton.signal_connect( 'clicked' ) { recup_entree 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 bouton_scanner = Gtk::Button.new( 'scanner' ) bouton_scanner.signal_connect( 'clicked' ) { recup_entree main_window.set_sensitive( false ) fenetre_scan main_window.destroy Gtk.main_quit } # bouton quitter hbox_boutons.pack_start( bouton_scanner, false, true, 0 ) bouton_annuler = Gtk::Button.new( 'quitter' ) bouton_annuler.signal_connect( 'clicked' ) { exit } # bouton valider hbox_boutons.pack_start( bouton_annuler, false, true, 0 ) bouton_ok = Gtk::Button.new( 'valider' ) bouton_ok.signal_connect( 'clicked' ) { recup_entree scan_done = true 2.times { |couv| valid_titres = $couvertures[ couv ].titres valid_titres.each { |nom| scan_done = false if nom == " " } } if scan_done == false fenetre_erreur( 'titre_manquant' ) if scan_done == false else $config.scan_ok end main_window.destroy Gtk.main_quit } hbox_boutons.pack_start( bouton_ok, false, true, 0 ) main_window.show_all Gtk.mainend#-----------------------------------------------------------------------------------------------------------------------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( "indiquez quel lecteur doit etre scanne\net a quelle pochette seront attribue\nses titres" ) 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.mainend#-----------------------------------------------------------------------------------------------------------------------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}` # on attend que le cd soit monté temps_depart = Time.now.sec temps_arrivee = temps_depart + 10 temps_arrivee -= 60 if temps_arrivee >= 60 while Time.now.sec != temps_arrivee; end #vérification de la présence des divix nombre_divix, titres = 0, Array.new Dir.foreach( $chemin_cd + lecteur.to_s ) { |fichier| fichier = fichier.downcase if fichier.match( 'avi' ) != nil || fichier.match( 'wmv' ) != nil || fichier.match( 'mpg' ) != nil $couvertures[ couverture ].set_titre( nombre_divix, fichier ) nombre_divix += 1 end } $couvertures[ couverture ].titres_sort fenetre_erreur( 'trop_de_fichiers' ) if nombre_divix > $config.nbr_titres? 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( $config.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 $config.set_fichier( id, nom ) window_fichier.destroy Gtk.main_quit } window_fichier.show_all Gtk.mainend#-----------------------------------------------------------------------------------------------------------------------# fenetre des erreursdef 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.\ncontinuer quand meme?" lbl_message = Gtk::Label.new( message_erreur ) main_box_erreur.pack_start( lbl_message, false, false, 0 ) bouton_erreur_oui = Gtk::Button.new( 'oui' ) main_box_erreur.pack_start( bouton_erreur_oui, false, true, 0 ) bouton_erreur_oui.signal_connect( 'clicked' ) { $config.scan_ok; window_erreur.destroy; Gtk.main_quit } when 'trop_de_fichiers' : message_erreur = "il y a plus de " + $config.nbr_titres? + "fichiers reconnus sur ce cd" lbl_message = Gtk::Label.new( message_erreur ) main_box_erreur.pack_start( lbl_message, false, false, 0 ) end bouton_erreur_revenir = Gtk::Button.new( 'revenir' ) main_box_erreur.pack_start( bouton_erreur_revenir, false, true, 0 ) bouton_erreur_revenir.signal_connect( 'clicked' ) { window_erreur.destroy; Gtk.main_quit } window_erreur.show_all Gtk.main end#-------------------------------------------------------------------------------------------------------------def recup_entree 2.times { |couv| entry = $entry_titres[ couv ] 6.times { |i| titre = entry[ i ].get_chars( 0, -1 ) $couvertures[ couv ].set_titre( i, titre ) } }end#on propose à l'utilisateur de visionner le résultatdef 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.mainend#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# séquence principale$config = Configuration.new2.times { |i| $couvertures[ i ] = Couverture.new( i, $config.nbr_titres? ) }#-----------------------------------------------------------------------------------------------------------------------#GUIwhile $config.scan? == false fenetre_principaleend#-----------------------------------------------------------------------------------------------------------------------#récupération du code du fichier modèle$lignes = IO.readlines( $config.fichiers?( 0 ) )#-----------------------------------------------------------------------------------------------------------------------#modification des lignes 2.times { |couv| titres = $couvertures[ couv ].titres 6.times { |i| pos = $couvertures[ couv ].position( i ) coord_y = $lignes[ pos ].split( '>' ) $lignes[ pos ] = coord_y[ 0 ] + ">" + titres[ i ] + "</tspan><tspan" $lignes[ pos ] = coord_y[ 0 ] + ">" + titres[ i ] + "</tspan></text>" if i == 5 && couv == 0 } } #-----------------------------------------------------------------------------------------------------------------------#on écrit les titres dans le fichier .svgfichier_nouveau = File.new( $config.fichiers?( 1 ), "w" ) fichier_nouveau.puts( $lignes )fichier_nouveau.close#-----------------------------------------------------------------------------------------------------------------------#finalisationfinirpath = $config.fichiers?( 1 )`inkscape #{$config.fichiers?( 1 )}`