1
// -------------------------------------------------------------------------------- //
2
// Copyright (C) 2008-2010 J.Rios
5
// This Program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
10
// This Program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; see the file LICENSE. If not, write to
17
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
// http://www.gnu.org/copyleft/gpl.h"tml
20
// -------------------------------------------------------------------------------- //
21
#include "FileRenamer.h"
30
// -------------------------------------------------------------------------------- //
31
guFileRenamer::guFileRenamer( wxWindow * parent, guDbLibrary * db, const wxArrayString &files )
35
m_CurFile = wxNOT_FOUND;
37
guConfig * Config = ( guConfig * ) guConfig::Get();
40
WindowPos.x = Config->ReadNum( wxT( "PosX" ), -1, wxT( "FileRenamer" ) );
41
WindowPos.y = Config->ReadNum( wxT( "PosY" ), -1, wxT( "FileRenamer" ) );
43
WindowSize.x = Config->ReadNum( wxT( "SizeWidth" ), 500, wxT( "FileRenamer" ) );
44
WindowSize.y = Config->ReadNum( wxT( "SizeHeight" ), 320, wxT( "FileRenamer" ) );
46
Create( parent, wxID_ANY, _( "Files Renaming" ), WindowPos, WindowSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
48
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
50
wxBoxSizer* MainSizer;
51
MainSizer = new wxBoxSizer( wxVERTICAL );
53
wxStaticBoxSizer * FilesSizer;
54
FilesSizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _(" Names Preview ") ), wxVERTICAL );
56
m_FilesListBox = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_EXTENDED|wxLB_MULTIPLE );
57
m_FilesListBox->Append( files );
58
FilesSizer->Add( m_FilesListBox, 1, wxEXPAND|wxALL, 5 );
60
wxFlexGridSizer * EditSizer;
61
EditSizer = new wxFlexGridSizer( 2, 2, 0, 0 );
62
EditSizer->AddGrowableCol( 1 );
63
EditSizer->SetFlexibleDirection( wxBOTH );
64
EditSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
66
wxStaticText * NameStaticText = new wxStaticText( this, wxID_ANY, _("Name:"), wxDefaultPosition, wxDefaultSize, 0 );
67
NameStaticText->Wrap( -1 );
68
EditSizer->Add( NameStaticText, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
70
m_NameTextCtrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
71
EditSizer->Add( m_NameTextCtrl, 0, wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT|wxEXPAND, 5 );
73
wxStaticText * PatternStaticText = new wxStaticText( this, wxID_ANY, _("Pattern:"), wxDefaultPosition, wxDefaultSize, 0 );
74
PatternStaticText->Wrap( -1 );
75
EditSizer->Add( PatternStaticText, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
77
wxBoxSizer* PatternSizer;
78
PatternSizer = new wxBoxSizer( wxHORIZONTAL );
80
m_PatTextCtrl = new wxTextCtrl( this, wxID_ANY, Config->ReadStr( wxT( "Pattern" ), wxT( "{n} - {a} - {t}" ), wxT( "FileRenamer" ) ), wxDefaultPosition, wxDefaultSize, 0 );
81
m_PatTextCtrl->SetToolTip( _("Pattern flags:\n{g} \t- Genero\n{a} \t- Artist Name\n{b} \t- Album Name\n{t} \t- Track Title\n{n} \t- Track Number\n{y} \t- Track Year\n{d} \t- Disk\n") );
83
PatternSizer->Add( m_PatTextCtrl, 1, wxTOP|wxBOTTOM|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5 );
85
m_PatApplyBtn = new wxBitmapButton( this, wxID_ANY, guImage( guIMAGE_INDEX_tiny_accept ), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
86
PatternSizer->Add( m_PatApplyBtn, 0, wxTOP|wxBOTTOM|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5 );
88
m_PatRevertBtn = new wxBitmapButton( this, wxID_ANY, guImage( guIMAGE_INDEX_tiny_reload ), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
89
PatternSizer->Add( m_PatRevertBtn, 0, wxTOP|wxBOTTOM|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5 );
91
EditSizer->Add( PatternSizer, 1, wxEXPAND, 5 );
93
FilesSizer->Add( EditSizer, 0, wxEXPAND, 5 );
95
MainSizer->Add( FilesSizer, 1, wxEXPAND|wxALL, 5 );
97
wxStdDialogButtonSizer * ButtonsSizer = new wxStdDialogButtonSizer();
98
wxButton * ButtonsSizerOK = new wxButton( this, wxID_OK );
99
ButtonsSizer->AddButton( ButtonsSizerOK );
100
wxButton * ButtonsSizerCancel = new wxButton( this, wxID_CANCEL );
101
ButtonsSizer->AddButton( ButtonsSizerCancel );
102
ButtonsSizer->Realize();
103
MainSizer->Add( ButtonsSizer, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
105
this->SetSizer( MainSizer );
108
Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnOKButton ) );
110
m_FilesListBox->Connect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( guFileRenamer::OnFileSelected ), NULL, this );
111
m_PatTextCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( guFileRenamer::OnPatternChanged ), NULL, this );
112
m_PatApplyBtn->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnPatternApply ), NULL, this );
113
m_PatRevertBtn->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnPattternRevert ), NULL, this );
116
// -------------------------------------------------------------------------------- //
117
guFileRenamer::~guFileRenamer()
119
guConfig * Config = ( guConfig * ) guConfig::Get();
120
// Save the window position and size
121
wxPoint WindowPos = GetPosition();
122
Config->WriteNum( wxT( "PosX" ), WindowPos.x, wxT( "FileRenamer" ) );
123
Config->WriteNum( wxT( "PosY" ), WindowPos.y, wxT( "FileRenamer" ) );
124
wxSize WindowSize = GetSize();
125
Config->WriteNum( wxT( "SizeWidth" ), WindowSize.x, wxT( "FileRenamer" ) );
126
Config->WriteNum( wxT( "SizeHeight" ), WindowSize.y, wxT( "FileRenamer" ) );
128
Config->WriteStr( wxT( "Pattern" ), m_PatTextCtrl->GetLineText( 0 ), wxT( "FileRenamer" ) );
130
m_FilesListBox->Disconnect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( guFileRenamer::OnFileSelected ), NULL, this );
131
m_PatTextCtrl->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( guFileRenamer::OnPatternChanged ), NULL, this );
132
m_PatApplyBtn->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnPatternApply ), NULL, this );
133
m_PatRevertBtn->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnPattternRevert ), NULL, this );
136
// -------------------------------------------------------------------------------- //
137
void guFileRenamer::OnOKButton( wxCommandEvent& event )
139
if( m_CurFile != wxNOT_FOUND )
141
if( m_NameTextCtrl->IsModified() )
143
m_FilesListBox->SetString( m_CurFile, m_NameTextCtrl->GetLineText( 0 ) );
149
// -------------------------------------------------------------------------------- //
150
void guFileRenamer::OnFileSelected( wxCommandEvent& event )
152
wxArrayInt Selection;
153
int SelCount = m_FilesListBox->GetSelections( Selection );
155
if( m_CurFile != wxNOT_FOUND )
157
if( m_NameTextCtrl->IsModified() )
159
m_FilesListBox->SetString( m_CurFile, m_NameTextCtrl->GetLineText( 0 ) );
163
m_NameTextCtrl->Enable( SelCount == 1 );
166
m_CurFile = Selection[ 0 ];
167
m_NameTextCtrl->SetValue( m_FilesListBox->GetString( m_CurFile ) );
171
m_CurFile = wxNOT_FOUND;
172
m_NameTextCtrl->SetValue( wxEmptyString );
176
// -------------------------------------------------------------------------------- //
177
void guFileRenamer::OnPatternChanged( wxCommandEvent& event )
179
bool Enabled = !m_PatTextCtrl->IsEmpty();
180
m_PatApplyBtn->Enable( Enabled );
181
m_PatRevertBtn->Enable( Enabled );
184
// -------------------------------------------------------------------------------- //
185
void guFileRenamer::OnPatternApply( wxCommandEvent& event )
187
wxArrayInt Selection;
188
wxArrayString RenameFiles;
190
int Count = m_FilesListBox->GetSelections( Selection );
193
for( Index = 0; Index < Count; Index++ )
195
RenameFiles.Add( m_Files[ Selection[ Index ] ] );
200
Count = m_Files.Count();
201
for( Index = 0; Index < Count; Index++ )
202
Selection.Add( Index );
203
RenameFiles = m_Files;
206
if( ( Count = RenameFiles.Count() ) )
208
for( Index = 0; Index < Count; Index++ )
210
wxString FileName = RenameFiles[ Index ];
211
if( guIsValidAudioFile( FileName ) )
213
wxURI Uri( FileName );
215
if( Uri.IsReference() )
217
guTrack * Track = new guTrack();
218
Track->m_FileName = FileName;
220
if( !m_Db->FindTrackFile( FileName, Track ) )
222
guPodcastItem PodcastItem;
223
if( m_Db->GetPodcastItemFile( FileName, &PodcastItem ) )
230
//guLogMessage( wxT( "Reading tags from the file..." ) );
231
guTagInfo * TagInfo = guGetTagInfoHandler( FileName );
234
Track->m_Type = guTRACK_TYPE_NOTDB;
238
Track->m_ArtistName = TagInfo->m_ArtistName;
239
Track->m_AlbumName = TagInfo->m_AlbumName;
240
Track->m_SongName = TagInfo->m_TrackName;
241
Track->m_Number = TagInfo->m_Track;
242
Track->m_GenreName = TagInfo->m_GenreName;
243
Track->m_Length = TagInfo->m_Length;
244
Track->m_Year = TagInfo->m_Year;
245
Track->m_Rating = wxNOT_FOUND;
246
Track->m_CoverId = 0;
252
guLogError( wxT( "Could not read tags from file '%s'" ), FileName.c_str() );
259
if( !m_PatTextCtrl->GetLineText( 0 ).StartsWith( wxT( "/" ) ) )
261
FileName = wxPathOnly( FileName ) + wxT( "/" ) + m_PatTextCtrl->GetLineText( 0 ) +
262
wxT( '.' ) + FileName.AfterLast( wxT( '.' ) );
266
FileName = m_PatTextCtrl->GetLineText( 0 ) +
267
wxT( '.' ) + FileName.AfterLast( wxT( '.' ) );
270
FileName = guExpandTrackMacros( FileName, Track );
272
m_FilesListBox->SetString( Selection[ Index ], FileName );
281
// -------------------------------------------------------------------------------- //
282
void guFileRenamer::OnPattternRevert( wxCommandEvent& event )
284
m_FilesListBox->Clear();
285
m_FilesListBox->Append( m_Files );
288
// -------------------------------------------------------------------------------- //
289
wxArrayString guFileRenamer::GetRenamedNames( void )
291
wxArrayString RetVal;
293
int Count = m_FilesListBox->GetCount();
294
for( Index = 0; Index < Count; Index++ )
296
RetVal.Add( m_FilesListBox->GetString( Index ) );
301
// -------------------------------------------------------------------------------- //