1 /***************************************************************************
2 * Copyright (C) 2008-2010 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 of the License, or *
8 * (at your option) any later version. *
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; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "tiny_tag_editor.h"
27 #include "vorbisfile.h"
36 #include "search_engine.h"
37 #include "tag_editor.h"
39 using Global::MainHeight
;
40 using Global::MainStartY
;
41 using Global::myOldScreen
;
43 TinyTagEditor
*myTinyTagEditor
= new TinyTagEditor
;
45 void TinyTagEditor::Init()
47 w
= new Menu
<Buffer
>(0, MainStartY
, COLS
, MainHeight
, "", Config
.main_color
, brNone
);
48 w
->HighlightColor(Config
.main_highlight_color
);
49 w
->CyclicScrolling(Config
.use_cyclic_scrolling
);
50 w
->SetItemDisplayer(Display::Generic
);
54 void TinyTagEditor::Resize()
56 w
->Resize(COLS
, MainHeight
);
57 w
->MoveTo(0, MainStartY
);
61 void TinyTagEditor::SwitchTo()
63 if (itsEdited
.isStream())
65 ShowMessage("Streams cannot be edited!");
71 myOldScreen
= Global::myScreen
;
72 Global::myScreen
= this;
73 Global::RedrawHeader
= 1;
77 std::string full_path
;
78 if (itsEdited
.isFromDB())
79 full_path
+= Config
.mpd_music_dir
;
80 full_path
+= itsEdited
.GetFile();
82 std::string message
= "Couldn't read file \"";
83 message
+= Shorten(TO_WSTRING(full_path
), COLS
-message
.length()-3);
85 ShowMessage("%s", message
.c_str());
89 std::basic_string
<my_char_t
> TinyTagEditor::Title()
91 return U("Tiny tag editor");
94 void TinyTagEditor::EnterPressed()
96 size_t option
= w
->Choice();
97 MPD::Song
&s
= itsEdited
;
100 if (option
< 19) // separator after comment
102 size_t pos
= option
-8;
103 Statusbar() << fmtBold
<< Info::Tags
[pos
].Name
<< ": " << fmtBoldEnd
;
104 s
.SetTags(Info::Tags
[pos
].Set
, Global::wFooter
->GetString(s
.GetTags(Info::Tags
[pos
].Get
)));
105 w
->at(option
).Clear();
106 w
->at(option
) << fmtBold
<< Info::Tags
[pos
].Name
<< ':' << fmtBoldEnd
<< ' ';
107 ShowTag(w
->at(option
), s
.GetTags(Info::Tags
[pos
].Get
));
109 else if (option
== 20)
111 Statusbar() << fmtBold
<< "Filename: " << fmtBoldEnd
;
112 std::string filename
= s
.GetNewName().empty() ? s
.GetName() : s
.GetNewName();
113 size_t dot
= filename
.rfind(".");
114 std::string extension
= filename
.substr(dot
);
115 filename
= filename
.substr(0, dot
);
116 std::string new_name
= Global::wFooter
->GetString(filename
);
117 s
.SetNewName(new_name
+ extension
);
118 w
->at(option
).Clear();
119 w
->at(option
) << fmtBold
<< "Filename:" << fmtBoldEnd
<< ' ' << (s
.GetNewName().empty() ? s
.GetName() : s
.GetNewName());
125 ShowMessage("Updating tags...");
126 if (TagEditor::WriteTags(s
))
128 ShowMessage("Tags updated!");
131 Mpd
.UpdateDirectory(locale_to_utf_cpy(s
.GetDirectory()));
132 if (myOldScreen
== mySearcher
) // songs from search engine are not updated automatically
133 *mySearcher
->Main()->Current().second
= s
;
137 if (myOldScreen
== myPlaylist
)
138 myPlaylist
->Items
->Current() = s
;
139 else if (myOldScreen
== myBrowser
)
140 myBrowser
->GetDirectory(myBrowser
->CurrentDir());
144 ShowMessage("Error while writing tags!");
147 myOldScreen
->SwitchTo();
150 void TinyTagEditor::MouseButtonPressed(MEVENT me
)
152 if (w
->Empty() || !w
->hasCoords(me
.x
, me
.y
) || size_t(me
.y
) >= w
->Size())
154 if (me
.bstate
& (BUTTON1_PRESSED
| BUTTON3_PRESSED
))
158 if (me
.bstate
& BUTTON3_PRESSED
)
165 Screen
< Menu
<Buffer
> >::MouseButtonPressed(me
);
168 bool TinyTagEditor::SetEdited(MPD::Song
*s
)
176 bool TinyTagEditor::GetTags()
178 MPD::Song
&s
= itsEdited
;
180 std::string path_to_file
;
182 path_to_file
+= Config
.mpd_music_dir
;
183 path_to_file
+= s
.GetFile();
184 locale_to_utf(path_to_file
);
186 TagLib::FileRef
f(path_to_file
.c_str());
189 s
.SetComment(f
.tag()->comment().to8Bit(1));
191 std::string ext
= s
.GetFile();
192 ext
= ext
.substr(ext
.rfind(".")+1);
203 for (size_t i
= 0; i
< 7; ++i
)
207 w
->IntoSeparator(19);
208 w
->IntoSeparator(21);
210 if (!extendedTagsSupported(f
.file()))
213 for (size_t i
= 15; i
<= 17; ++i
)
219 w
->at(0) << fmtBold
<< Config
.color1
<< "Song name: " << fmtBoldEnd
<< Config
.color2
<< s
.GetName() << clEnd
;
220 w
->at(1) << fmtBold
<< Config
.color1
<< "Location in DB: " << fmtBoldEnd
<< Config
.color2
;
221 ShowTag(w
->at(1), s
.GetDirectory());
223 w
->at(3) << fmtBold
<< Config
.color1
<< "Length: " << fmtBoldEnd
<< Config
.color2
<< s
.GetLength() << clEnd
;
224 w
->at(4) << fmtBold
<< Config
.color1
<< "Bitrate: " << fmtBoldEnd
<< Config
.color2
<< f
.audioProperties()->bitrate() << " kbps" << clEnd
;
225 w
->at(5) << fmtBold
<< Config
.color1
<< "Sample rate: " << fmtBoldEnd
<< Config
.color2
<< f
.audioProperties()->sampleRate() << " Hz" << clEnd
;
226 w
->at(6) << fmtBold
<< Config
.color1
<< "Channels: " << fmtBoldEnd
<< Config
.color2
<< (f
.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << clDefault
;
229 for (const Info::Metadata
*m
= Info::Tags
; m
->Name
; ++m
, ++pos
)
231 w
->at(pos
) << fmtBold
<< m
->Name
<< ":" << fmtBoldEnd
<< ' ';
232 ShowTag(w
->at(pos
), s
.GetTags(m
->Get
));
235 w
->at(20) << fmtBold
<< "Filename:" << fmtBoldEnd
<< ' ' << s
.GetName();
238 w
->at(23) << "Cancel";
242 bool TinyTagEditor::extendedTagsSupported(TagLib::File
*f
)
244 return dynamic_cast<TagLib::MPEG::File
*>(f
)
245 || dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
)
246 || dynamic_cast<TagLib::FLAC::File
*>(f
);
249 #endif // HAVE_TAGLIB_H