Passage à ActiveRecord::Migration pour la création et la gestion de la base de données.
[bilio:bilio.git] / main.rb
1 # coding: utf-8
2
3 class MainWindow < Gtk::Window
4         
5                 attr_reader :type_doc, :login, :tableau_bord, :document, :article, :tiers, :liste_documents, :liste_users, :liste_articles, :liste_tiers, :version, :utilisateur, :liste_lots, :transformation
6         attr_accessor :conf
7         
8         def initialize level
9         
10                 super
11                 
12                 signal_connect( "destroy" ) { Gtk.main_quit }
13                 
14                 @version = "0.2"
15                 set_title GLib.application_name + " v" + @version
16                 
17                 set_window_position Gtk::Window::POS_CENTER
18                 set_default_size 900, 300
19                 set_icon( "./resources/icons/icon.png" )
20                 
21                 @conf = YAML.load_file("configuration.yaml")
22                 
23                 ActiveRecord::Base.establish_connection(@conf)
24                 ActiveRecord::Base.logger = Logger.new(File.open('db/database.log','w'))
25                 
26                 @type_doc = TypeDocument_hash.new self
27                 
28                 @toolbargen = ToolBarGen_box.new self
29                 @login = Login_box.new self
30                 
31                 @vbox = Gtk::VBox.new false, 2
32                 @vbox.pack_start @toolbargen, false, false, 2
33                 @vbox.pack_start @login, false, false, 2
34                 @login.focus
35                 
36                 @tableau_bord = TableauBord_box.new self
37                 @document = Document_box.new self
38                 @article = Article_box.new self
39                 @tiers = Tiers_box.new self
40                 @liste_articles = ListeArticles_box.new self
41                 @liste_documents = ListeDocuments_box.new self
42                 @liste_users = ListeUsers_box.new self
43                 @liste_tiers = ListeTiers_box.new self
44                 @utilisateur = Utilisateur_box.new self
45                 @liste_lots = ListeLots_box.new self
46                 @transformation = Transformation_box.new self
47                 
48                 add @vbox
49                 
50                 show_all
51         
52         end
53         
54         def active_menu
55         
56                 @toolbargen.each { |child|
57                         child.sensitive = true
58                 }
59                 
60                 # If not a admin, config menu is hidden
61                 @toolbargen.children[7].sensitive = false unless @login.user.admin
62         
63         end
64         
65         def efface_vbox_actuelle
66         
67                 @vbox.remove @vbox.children[@vbox.children.count-1] if @vbox.children.count.eql?(2)
68         
69         end
70         
71         def affiche vbox
72         
73                 efface_vbox_actuelle
74                 @vbox.pack_start vbox, true
75         self.show_all
76         
77         end     
78         
79         def quit
80                 
81                 dialog = Gtk::MessageDialog.new(self, 
82                                 Gtk::Dialog::DESTROY_WITH_PARENT,
83                                 Gtk::MessageDialog::QUESTION,
84                                 Gtk::MessageDialog::BUTTONS_YES_NO,
85                                 "Voulez-vous réellement quitter #{GLib.application_name.capitalize} ?")
86                 response = dialog.run
87                 case response
88                   when Gtk::Dialog::RESPONSE_YES
89                         Gtk.main_quit
90                 end 
91                 dialog.destroy
92                 
93         end
94         
95         def message_config
96         
97                 dialog = Gtk::Dialog.new("Configuration",
98                              @window,
99                              Gtk::Dialog::DESTROY_WITH_PARENT,
100                              [ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK ],
101                              [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ]
102                              )
103
104                 vbox = Gtk::VBox.new false, 2
105                 hboxserver = Gtk::HBox.new false, 2
106                 hboxdb_type = Gtk::HBox.new false, 2
107                 hboxdb = Gtk::HBox.new false, 2
108                 hboxdb_user = Gtk::HBox.new false, 2
109                 hboxdb_password = Gtk::HBox.new false, 2
110                 framedb = Gtk::Frame.new "Configuration de la base de données"
111                 vboxdb = Gtk::VBox.new false, 2
112                 vboxdb.add hboxdb_type
113                 vboxdb.add hboxserver
114                 vboxdb.add hboxdb
115                 vboxdb.add hboxdb_user
116                 vboxdb.add hboxdb_password
117                 framedb.add vboxdb
118                 vbox.add framedb        
119                 
120                 hboxdb_type.pack_start( Gtk::Label.new( "Type de serveur :" ), false, false, 3 )
121                 db_type = Gtk::ComboBox.new
122                 db_type.append_text @conf["adapter"]
123                 db_type.active = 0
124                 hboxdb_type.pack_start( db_type, false, false, 3 )
125                 
126                 hboxserver.pack_start( Gtk::Label.new( "Adresse du serveur :" ), false, false, 3 )
127                 serveur = Gtk::Entry.new
128                 serveur.text = @conf["host"] unless @conf["host"].nil?
129                 hboxserver.pack_start( serveur, false, false, 3 )
130                 
131                 hboxdb.pack_start( Gtk::Label.new( "Nom de la base de données :" ), false, false, 3 )
132                 db = Gtk::Entry.new
133                 db.text = @conf["database"] unless @conf["database"].nil?
134                 hboxdb.pack_start( db, false, false, 3 )
135                 
136                 hboxdb_user.pack_start( Gtk::Label.new( "Utilisateur de la base de données :" ), false, false, 3 )
137                 db_user = Gtk::Entry.new
138                 db_user.text = @conf["username"] unless @conf["username"].nil?
139                 hboxdb_user.pack_start( db_user, false, false, 3 )
140                 
141                 hboxdb_password.pack_start( Gtk::Label.new( "Mot de passe de la base de données :" ), false, false, 3 )
142                 db_password = Gtk::Entry.new
143                 db_password.text = @conf["password"] unless @conf["password"].nil?
144                 db_password.visibility = false
145                 hboxdb_password.pack_start( db_password, false, false, 3 )
146                 
147                 
148                 dialog.vbox.add(vbox)
149                 vbox.show_all
150                 dialog.run { |response| 
151                         if response==Gtk::Dialog::RESPONSE_OK then
152                                 
153                                 @conf["adapter"] = db_type.active_text
154                                 @conf["host"] = serveur.text
155                                 @conf["database"] = db.text
156                                 @conf["username"] = db_user.text
157                                 @conf["password"] = db_password.text
158                                 output = File.new('configuration.yaml', 'w')
159                                 output.puts YAML.dump(@conf)
160                                 output.close
161                                 
162                         else
163                                 @quitconfig = true
164                         end
165                         
166                         dialog.destroy
167                 }       
168         
169         end
170         
171         def message_erreur message
172                 m = "Impossible d'exécuter la requête demandée...\nVoici le message d'erreur affiché :\n" + message
173                 dialog = Gtk::MessageDialog.new(self, 
174                                 Gtk::Dialog::DESTROY_WITH_PARENT,
175                                 Gtk::MessageDialog::ERROR,
176                                 Gtk::MessageDialog::BUTTONS_OK,
177                                 m)
178                 response = dialog.run
179                 dialog.destroy
180         end
181
182 end