/MainWindow.rb
Ruby | 397 lines | 346 code | 32 blank | 19 comment | 33 complexity | 2d47e9dc15249b924ff3504cdae26008 MD5 | raw file
Possible License(s): GPL-2.0
- # Main Window
- # Copyright (C) <2008-2010> Abdelrahman ghanem <abom.jdev@gmail.com>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License along
- # with this program; if not, write to the Free Software Foundation, Inc.,
- # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- require 'rubygems'
- require 'lib/quren'
- require 'lib/tagger'
- require 'lib/arabic_normalization'
- require 'UiMainWindow'
- require 'LanguagesDialog'
- require 'EditTagDialog'
- class MWindow < Qt::MainWindow
- slots 'rename()',
- 'preview(bool)',
- 'update_preview(int)',
- 'browse()',
- 'edit_tags()',
- 'lang()',
- 'about()',
- 'load_files!(int)',
- 'view_file_name()',
- 'play()',
- 'stop()',
- 'opendir_fromtree()',
- 'reload_paths(const QString&)'
-
- def initialize parent=nil
- super parent
- @mwin=Ui_MainWindow.new
- @mwin.setupUi self
- @mwin.pb.visible=false
- @mwin.lstfiles.setColumnHidden 2,true
- @mwin.lstfiles.setColumnHidden 3,true
-
- @quren=QuRen.new
- @tagger=Tagger.new
- @langdialog=LanguagesDialog.new
- load_langs!
- ok?
-
- connect(@mwin.comboext,SIGNAL('currentIndexChanged(int)'),
- self,SLOT('load_files!(int)'))
- connect(@mwin.combolang, SIGNAL('currentIndexChanged(int)'),
- self, SLOT('update_preview(int)'))
- connect(@mwin.action_Preview,SIGNAL('toggled(bool)'),
- self,SLOT('preview(bool)'))
- connect(@mwin.lstfiles,SIGNAL('itemSelectionChanged()'),
- self,SLOT('view_file_name()'))
- connect(@mwin.btnbrowse,SIGNAL('clicked()'),
- self,SLOT('browse()'))
- connect(@mwin.btnopendir,SIGNAL('clicked()'),
- self,SLOT('opendir_fromtree()'))
- connect(@mwin.action_rename,SIGNAL('triggered()'),
- self,SLOT('rename()'))
- connect(@mwin.action_Edit_Tags,SIGNAL('triggered()'),
- self,SLOT('edit_tags()'))
- connect(@mwin.action_Languages,SIGNAL('triggered()'),
- self,SLOT('lang()'))
- connect(@mwin.action_About,SIGNAL('triggered()'),
- self,SLOT('about()'))
- connect(@mwin.action_Play,SIGNAL('triggered()'),
- self,SLOT('play()'))
- connect(@mwin.action_Stop,SIGNAL('triggered()'),
- self,SLOT('stop()'))
-
- @player=Phonon::MediaObject.new
- @audio_output=Phonon::AudioOutput.new(Phonon::MusicCategory)
-
- Phonon.createPath(@player,@audio_output)
- @player.tickInterval=1000
- connect(@player, SIGNAL('finished()'),
- self, SLOT('stop()'))
-
- @mwin.lstfiles.sortItems(0,Qt::AscendingOrder)
- @mwin.action_Stop.enabled=false
-
- @mwin.treeDirs.header.defaultSectionSize=200
- @dir_model=Qt::FileSystemModel.new
- @dir_model.setFilter(Qt::Dir::AllDirs)
- @mwin.treeDirs.model=@dir_model
- @filesystemwatcher=Qt::FileSystemWatcher.new
- @dir_model.rootPath='/'
- connect(@filesystemwatcher,SIGNAL('directoryChanged(const QString&)'),
- self,SLOT('reload_paths(const QString&)'))
- end
-
- def load_langs!
- @mwin.combolang.clear
- files=@quren.get_files 'langs','xml'
- files.each do |file|
- if File.extname(file)=='.xml'
- @mwin.combolang.addItem file.gsub('.xml','')
- end
- end
- end
-
- def load_exts!
- current_ext_idx = @mwin.comboext.currentIndex
- @mwin.comboext.clear
- exts=@quren.get_exts @mwin.txtpath.text
- return if exts.nil?
- exts.each do |ext|
- @mwin.comboext.addItem ext
- end
- @mwin.comboext.currentIndex = current_ext_idx unless current_ext_idx==-1
- end
-
- def ok?
- dir=@mwin.txtpath.text
- ext=@mwin.comboext.currentText
- if dir!='' and !ext.nil?
- en_actions true
- return true
- else
- en_actions false
- return false
- end
- end
-
- def load_files! idx=0
- ok?
- @mwin.lstfiles.clear
- @files=@quren.get_files @mwin.txtpath.text,@mwin.comboext.currentText
- return if @files.length==0
- idx=0
- @files.each do |file|
- item=Qt::TreeWidgetItem.new @mwin.lstfiles
- idx+=1
- item.setText 0,idx.to_s
- item.setText 1,file
- end
- re_cols
- statusBar.showMessage(tr("Files with extension #{@mwin.comboext.currentText} loaded"))
- end
-
- def stop_fs_watch(&block)
- dirs=@filesystemwatcher.directories
- #puts dirs
- #FIXME: can't stop fs watch,
- #alot of 'return' in block exits the scope, so
- #addPaths dosn't emitted
- @filesystemwatcher.removePaths(dirs)
- block.call
- @filesystemwatcher.addPaths(dirs)
- end
-
- def open_dir(dir)
- prev_dir=@mwin.txtpath.text
- if @filesystemwatcher.directories.at(0)==prev_dir
- @filesystemwatcher.removePath(prev_dir)
- end
- @filesystemwatcher.addPath(dir)
- index=@dir_model.index(dir)
- @mwin.treeDirs.currentIndex=index
- @mwin.treeDirs.scrollTo(index,Qt::AbstractItemView::PositionAtCenter)
- @mwin.txtpath.text=dir
- load_exts!
- load_files!
- end
-
- def reload_paths(changed)
- load_exts!
- load_files!
- end
-
- def browse
- dir=Qt::FileDialog.getExistingDirectory self,tr('Choose Directory'),'/'
- return if dir.nil?
- open_dir(dir)
- end
-
- def opendir_fromtree
- selected=@mwin.treeDirs.selectedIndexes
- if selected.count>0
- path=@dir_model.filePath(selected.at(0))
- if File.directory?(path)
- open_dir(path)
- else
- open_dir(File.dirname(path))
- end
- end
- end
-
- def re_cols
- for i in 0..4
- @mwin.lstfiles.resizeColumnToContents(i)
- end
- end
-
- def preview bool
- return if !ok?
- @quren.workd=@mwin.txtpath.text
- @quren.langf="langs/#{@mwin.combolang.currentText}.xml"
- @quren.ext=@mwin.comboext.currentText
- @quren.sep=@mwin.txtsep.text
- if bool
- prev=@quren.do_ren(true)
- return if !@quren.ok?
- statusBar.showMessage(tr("Preview...."))
- @mwin.action_Edit_Tags.enabled=false
- @mwin.action_rename.enabled=false
- @mwin.lstfiles.clear
- @mwin.lstfiles.setColumnHidden 2,false
- @mwin.lstfiles.setColumnHidden 3,false
- @mwin.lstfiles.setColumnHidden 0,true
- @mwin.lstfiles.setColumnHidden 1,true
- prev.each do |key,value|
- item=Qt::TreeWidgetItem.new(@mwin.lstfiles)
- item.setText 2,key
- item.setText 3,value
- end
- re_cols
- else
- @mwin.action_Edit_Tags.enabled=true
- @mwin.action_rename.enabled=true
- @mwin.lstfiles.setColumnHidden 2,true
- @mwin.lstfiles.setColumnHidden 3,true
- @mwin.lstfiles.setColumnHidden 0,false
- @mwin.lstfiles.setColumnHidden 1,false
- load_files!
- end
- end
-
- def update_preview(index)
- if @mwin.action_Preview.checked
- preview(true)
- end
- end
-
- def view_file_name
- item=@mwin.lstfiles.currentItem
- return if item.nil?
- if @mwin.lstfiles.isColumnHidden(0)
- statusBar.showMessage(item.text(2)+tr(' Will be..... ')+item.text(3))
- return
- end
- statusBar.showMessage(File.join(@mwin.txtpath.text,item.text(1))) if item.text(1)
- end
-
- def rename
- stop_fs_watch do
- return if !ok?
- @quren.workd=@mwin.txtpath.text
- @quren.langf="langs/#{@mwin.combolang.currentText}.xml"
- @quren.ext=@mwin.comboext.currentText
- @quren.sep=@mwin.txtsep.text
- fcount=@quren.get_files.length
- return if !@quren.ok?
- @mwin.pb.visible=true
- @mwin.pb.maximum=fcount
- @quren.do_ren do |i|
- Qt::Application.processEvents
- @mwin.pb.value=i
- end
- @mwin.pb.visible=false
- load_files!
- statusBar.showMessage(tr("Done, #{fcount} file(s) renamed."))
- end
- end
-
- def lang
- @langdialog.exec
- load_langs!
- if @mwin.action_Preview.checked
- preview(true)
- end
- end
-
- def can_edit_for_all?
- @quren.ok?
- end
-
- def edit_tags
- stop_fs_watch do
- @tagdialog=EditTagDialog.new self
- item=@mwin.lstfiles.currentItem
- return if !ok? or item.nil?
- @quren.workd=@mwin.txtpath.text
- @quren.langf="langs/#{@mwin.combolang.currentText}.xml"
- @quren.ext=@mwin.comboext.currentText
- file=item.text(1)
- fpath=File.join @quren.workd,file
- if @quren.ext.downcase=='mp3' or @quren.ext.downcase=='ogg'
- @tagger.file=fpath
- @tagger.get!
- if !@tagger.vaild?
- Qt::MessageBox.critical(self,"Error",
- tr("File is invaild or has no tag!"))
- return
- end
- @tagdialog.file=fpath
- @tagdialog.clear
- @tagdialog.fill_fields!
- @tagdialog.exec
- else
- Qt::MessageBox.information(self,tr("Can't be!"),tr('You can edit tags for ogg and mp3 only!'))
- end
- if !can_edit_for_all?
- Qt::MessageBox.warning(self,tr('Apply for all'),
- tr("Can't apply changes for all!, you may rename them first."))
- return
- end
- if @tagdialog.result==Qt::Dialog::Accepted
- if @tagdialog.apply_for_all
- prog=0
- fcount=@quren.get_files.length
- @mwin.pb.visible=true
- @mwin.pb.maximum=fcount
- @quren.get_files.each do |file|
- Qt::Application.processEvents
- statusBar.showMessage(tr("Saving tags to... #{file}"))
- idx=@quren.idx_from_file file
- sura=@quren.sura_from_idx @quren.tidx(idx)
- sura=idx+" #{@mwin.txtsep.text} "+sura
- fpath=File.join @quren.workd,file
- @tagger.file=fpath
- @tagger.title=sura
- @tagger.artist=@tagdialog.shiekh
- @tagger.album=@tagdialog.mushaf
- @tagger.year=@tagdialog.year
- @tagger.comment=@tagdialog.comment
- @tagger.cover_image=@tagdialog.cover_image
- @tagger.cover_image_desc=@tagdialog.cover_image_desc
- @tagger.mimetype=@tagdialog.mimetype
- @tagger.set!
- prog+=1
- @mwin.pb.value=prog
- end
- statusBar.showMessage(tr("Done, tags saved to #{fcount} file(s)."))
- end
- end
- @mwin.pb.visible=false
- end
- end
-
- def en_actions bool
- if bool
- @mwin.action_rename.enabled=true
- @mwin.action_Preview.enabled=true
- @mwin.action_Edit_Tags.enabled=true
- else
- @mwin.action_rename.enabled=false
- @mwin.action_Preview.enabled=false
- @mwin.action_Edit_Tags.enabled=false
- end
- end
-
- def play
- item=@mwin.lstfiles.currentItem
- return if item.nil?
- file=File.join(@mwin.txtpath.text,item.text(1))
- if File.exists?(file)
- @player.currentSource=Phonon::MediaSource.new(file)
- @player.play
- @mwin.action_Play.enabled=false
- @mwin.action_Stop.enabled=true
- end
- end
-
- def stop
- if @player.currentSource.fileName
- @player.stop
- @mwin.action_Play.enabled=true
- @mwin.action_Stop.enabled=false
- end
- end
-
- def about
- Qt::MessageBox.about(self,tr("About"),
- tr("<b>QuRename</b> <br/>Version: 0.1.9<br/> <br/>License: GPL V2 or Later<br/><br/>
- <br/>Repo: <a href='http://bitbucket.org/abom/qurename/'>bitbucket.org/abom/qurename/</a><br/>
- <br/>QuRename is a tool to rename the quran files
- in one click,instead of renaming them one by one,
- for ex: rename 001.mp3 to 001 - Al-Al-Fatiha.mp3...etc,
- also it can edit tags for ogg and mp3 file formats.<br/>
- <br/>Copyright (c) 2008-2010 Abdelrahman Ghanem (abom)
- <br><a href='mailto:abom.jdev@gmail.com'>abom.jdev@gmail.com</a>
- <br><br><b>Thanks to:</b><br>Ahmed Youssef (StrikerX)
- <a href='mailto:xmonader@gmail.com'>xmonader@gmail.com</a><br>
- English Sewar Names, and more!"))
- end
-
- end