~ubuntu-branches/ubuntu/precise/guayadeque/precise

« back to all changes in this revision

Viewing changes to src/.svn/text-base/FileRenamer.cpp.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-05-14 15:08:03 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110514150803-8b5evqetnaj35j34
Tags: 0.3.1~dfsg0-1
* New upstream release.
* Strip wxsqlite3 stuff out of upstream's tarballs.
* Update get-orig-source target in debian/rules.
* Update gbp config file.
* Bump Standards.
* Build-depend on libwxsqlite3-2.8-dev
* Enable parallel builds.
* Link binaries against the system-wide copy of wxsqlite3.
* Point sources to the correct wxcurl's headers location.
* Update copyright file as per DEP-5
* Improve debian/watch to handle the ~dfsg\d* suffix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -------------------------------------------------------------------------------- //
2
 
//      Copyright (C) 2008-2010 J.Rios
3
 
//      anonbeat@gmail.com
4
 
//
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)
8
 
//    any later version.
9
 
//
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.
14
 
//
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
19
 
//
20
 
// -------------------------------------------------------------------------------- //
21
 
#include "FileRenamer.h"
22
 
 
23
 
#include "Config.h"
24
 
#include "Images.h"
25
 
#include "TagInfo.h"
26
 
#include "Utils.h"
27
 
 
28
 
#include <wx/uri.h>
29
 
 
30
 
// -------------------------------------------------------------------------------- //
31
 
guFileRenamer::guFileRenamer( wxWindow * parent, guDbLibrary * db, const wxArrayString &files )
32
 
{
33
 
    m_Db = db;
34
 
    m_Files = files;
35
 
    m_CurFile = wxNOT_FOUND;
36
 
 
37
 
    guConfig * Config = ( guConfig * ) guConfig::Get();
38
 
 
39
 
    wxPoint WindowPos;
40
 
    WindowPos.x = Config->ReadNum( wxT( "PosX" ), -1, wxT( "FileRenamer" ) );
41
 
    WindowPos.y = Config->ReadNum( wxT( "PosY" ), -1, wxT( "FileRenamer" ) );
42
 
    wxSize WindowSize;
43
 
    WindowSize.x = Config->ReadNum( wxT( "SizeWidth" ), 500, wxT( "FileRenamer" ) );
44
 
    WindowSize.y = Config->ReadNum( wxT( "SizeHeight" ), 320, wxT( "FileRenamer" ) );
45
 
 
46
 
    Create( parent, wxID_ANY, _( "Files Renaming" ), WindowPos, WindowSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
47
 
 
48
 
        this->SetSizeHints( wxDefaultSize, wxDefaultSize );
49
 
 
50
 
        wxBoxSizer* MainSizer;
51
 
        MainSizer = new wxBoxSizer( wxVERTICAL );
52
 
 
53
 
        wxStaticBoxSizer * FilesSizer;
54
 
        FilesSizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _(" Names Preview ") ), wxVERTICAL );
55
 
 
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 );
59
 
 
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 );
65
 
 
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 );
69
 
 
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 );
72
 
 
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 );
76
 
 
77
 
        wxBoxSizer* PatternSizer;
78
 
        PatternSizer = new wxBoxSizer( wxHORIZONTAL );
79
 
 
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") );
82
 
 
83
 
        PatternSizer->Add( m_PatTextCtrl, 1, wxTOP|wxBOTTOM|wxRIGHT|wxALIGN_CENTER_VERTICAL, 5 );
84
 
 
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 );
87
 
 
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 );
90
 
 
91
 
        EditSizer->Add( PatternSizer, 1, wxEXPAND, 5 );
92
 
 
93
 
        FilesSizer->Add( EditSizer, 0, wxEXPAND, 5 );
94
 
 
95
 
        MainSizer->Add( FilesSizer, 1, wxEXPAND|wxALL, 5 );
96
 
 
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 );
104
 
 
105
 
        this->SetSizer( MainSizer );
106
 
        this->Layout();
107
 
 
108
 
        Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( guFileRenamer::OnOKButton ) );
109
 
 
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 );
114
 
}
115
 
 
116
 
// -------------------------------------------------------------------------------- //
117
 
guFileRenamer::~guFileRenamer()
118
 
{
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" ) );
127
 
 
128
 
        Config->WriteStr( wxT( "Pattern" ), m_PatTextCtrl->GetLineText( 0 ), wxT( "FileRenamer" ) );
129
 
 
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 );
134
 
}
135
 
 
136
 
// -------------------------------------------------------------------------------- //
137
 
void guFileRenamer::OnOKButton( wxCommandEvent& event )
138
 
{
139
 
    if( m_CurFile != wxNOT_FOUND )
140
 
    {
141
 
        if( m_NameTextCtrl->IsModified() )
142
 
        {
143
 
            m_FilesListBox->SetString( m_CurFile, m_NameTextCtrl->GetLineText( 0 ) );
144
 
        }
145
 
    }
146
 
    event.Skip();
147
 
}
148
 
 
149
 
// -------------------------------------------------------------------------------- //
150
 
void guFileRenamer::OnFileSelected( wxCommandEvent& event )
151
 
{
152
 
    wxArrayInt Selection;
153
 
    int SelCount = m_FilesListBox->GetSelections( Selection );
154
 
 
155
 
    if( m_CurFile != wxNOT_FOUND )
156
 
    {
157
 
        if( m_NameTextCtrl->IsModified() )
158
 
        {
159
 
            m_FilesListBox->SetString( m_CurFile, m_NameTextCtrl->GetLineText( 0 ) );
160
 
        }
161
 
    }
162
 
 
163
 
    m_NameTextCtrl->Enable( SelCount == 1 );
164
 
    if( SelCount == 1 )
165
 
    {
166
 
        m_CurFile = Selection[ 0 ];
167
 
        m_NameTextCtrl->SetValue( m_FilesListBox->GetString( m_CurFile ) );
168
 
    }
169
 
    else
170
 
    {
171
 
        m_CurFile = wxNOT_FOUND;
172
 
        m_NameTextCtrl->SetValue( wxEmptyString );
173
 
    }
174
 
}
175
 
 
176
 
// -------------------------------------------------------------------------------- //
177
 
void guFileRenamer::OnPatternChanged( wxCommandEvent& event )
178
 
{
179
 
    bool Enabled = !m_PatTextCtrl->IsEmpty();
180
 
    m_PatApplyBtn->Enable( Enabled );
181
 
    m_PatRevertBtn->Enable( Enabled );
182
 
}
183
 
 
184
 
// -------------------------------------------------------------------------------- //
185
 
void guFileRenamer::OnPatternApply( wxCommandEvent& event )
186
 
{
187
 
    wxArrayInt Selection;
188
 
    wxArrayString RenameFiles;
189
 
    int Index;
190
 
    int Count = m_FilesListBox->GetSelections( Selection );
191
 
    if( Count )
192
 
    {
193
 
        for( Index = 0; Index < Count; Index++ )
194
 
        {
195
 
            RenameFiles.Add( m_Files[ Selection[ Index ] ] );
196
 
        }
197
 
    }
198
 
    else
199
 
    {
200
 
        Count = m_Files.Count();
201
 
        for( Index = 0; Index < Count; Index++ )
202
 
            Selection.Add( Index );
203
 
        RenameFiles = m_Files;
204
 
    }
205
 
 
206
 
    if( ( Count = RenameFiles.Count() ) )
207
 
    {
208
 
        for( Index = 0; Index < Count; Index++ )
209
 
        {
210
 
            wxString FileName = RenameFiles[ Index ];
211
 
            if( guIsValidAudioFile( FileName  ) )
212
 
            {
213
 
                wxURI Uri( FileName );
214
 
 
215
 
                if( Uri.IsReference() )
216
 
                {
217
 
                    guTrack * Track = new guTrack();
218
 
                    Track->m_FileName = FileName;
219
 
 
220
 
                    if( !m_Db->FindTrackFile( FileName, Track ) )
221
 
                    {
222
 
                        guPodcastItem PodcastItem;
223
 
                        if( m_Db->GetPodcastItemFile( FileName, &PodcastItem ) )
224
 
                        {
225
 
                            delete Track;
226
 
                            continue;
227
 
                        }
228
 
                        else
229
 
                        {
230
 
                            //guLogMessage( wxT( "Reading tags from the file..." ) );
231
 
                            guTagInfo * TagInfo = guGetTagInfoHandler( FileName );
232
 
                            if( TagInfo )
233
 
                            {
234
 
                                Track->m_Type = guTRACK_TYPE_NOTDB;
235
 
 
236
 
                                TagInfo->Read();
237
 
 
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;
247
 
 
248
 
                                delete TagInfo;
249
 
                            }
250
 
                            else
251
 
                            {
252
 
                                guLogError( wxT( "Could not read tags from file '%s'" ), FileName.c_str() );
253
 
                                delete Track;
254
 
                                continue;
255
 
                            }
256
 
                        }
257
 
                    }
258
 
 
259
 
                    if( !m_PatTextCtrl->GetLineText( 0 ).StartsWith( wxT( "/" ) ) )
260
 
                    {
261
 
                        FileName = wxPathOnly( FileName ) + wxT( "/" ) + m_PatTextCtrl->GetLineText( 0 ) +
262
 
                                    wxT( '.' ) + FileName.AfterLast( wxT( '.' ) );
263
 
                    }
264
 
                    else
265
 
                    {
266
 
                        FileName = m_PatTextCtrl->GetLineText( 0 ) +
267
 
                                    wxT( '.' ) + FileName.AfterLast( wxT( '.' ) );
268
 
                    }
269
 
 
270
 
                    FileName = guExpandTrackMacros( FileName, Track );
271
 
 
272
 
                    m_FilesListBox->SetString( Selection[ Index ], FileName );
273
 
 
274
 
                    delete Track;
275
 
                }
276
 
            }
277
 
        }
278
 
    }
279
 
}
280
 
 
281
 
// -------------------------------------------------------------------------------- //
282
 
void guFileRenamer::OnPattternRevert( wxCommandEvent& event )
283
 
{
284
 
    m_FilesListBox->Clear();
285
 
    m_FilesListBox->Append( m_Files );
286
 
}
287
 
 
288
 
// -------------------------------------------------------------------------------- //
289
 
wxArrayString guFileRenamer::GetRenamedNames( void )
290
 
{
291
 
    wxArrayString RetVal;
292
 
    int Index;
293
 
    int Count = m_FilesListBox->GetCount();
294
 
    for( Index = 0; Index < Count; Index++ )
295
 
    {
296
 
        RetVal.Add( m_FilesListBox->GetString( Index ) );
297
 
    }
298
 
    return RetVal;
299
 
}
300
 
 
301
 
// -------------------------------------------------------------------------------- //
302