| 1 | /* |
| 2 | Copyright (C) 2012 Harald Sitter <sitter@kde.org> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifndef PHONON_VLC_VSTRING_H |
| 19 | #define PHONON_VLC_VSTRING_H |
| 20 | |
| 21 | #include <QtCore/QString> |
| 22 | #include <vlc/libvlc.h> |
| 23 | #include <vlc/libvlc_version.h> |
| 24 | |
| 25 | /** |
| 26 | * @brief The VString class wraps around a char* returned from libvlc functions. |
| 27 | * Directly from libvlc functions returned cstrings are unique in two ways. |
| 28 | * For one they are to be freed by the caller, and for another they are always |
| 29 | * UTF8 (as VLC internally only uses UTF8). |
| 30 | * |
| 31 | * Particularly the first point is where VString comes in, it will on destruction |
| 32 | * call libvlc_free to free the string, thus avoiding memleaks. |
| 33 | * Additionally it conveniently converts to QString using either toQString |
| 34 | * or implicit cast to QString which makes it completely transparent to other |
| 35 | * functions. It also prevents you from carrying the cstring out of scope and |
| 36 | * render implicit copies of it invalid once free is called somewhere. |
| 37 | * Both functions use QString::fromUtf8 and are therefore the best way to |
| 38 | * process the string. |
| 39 | */ |
| 40 | class VString |
| 41 | { |
| 42 | public: |
| 43 | explicit VString(char *vlcString) : m_vlcString(vlcString) {} |
| 44 | ~VString() |
| 45 | { |
| 46 | libvlc_free(ptr: m_vlcString); |
| 47 | } |
| 48 | |
| 49 | // VLC internally only uses UTF8! |
| 50 | QString toQString() { return QString::fromUtf8(utf8: m_vlcString); } |
| 51 | operator QString() { return toQString(); } |
| 52 | |
| 53 | private: |
| 54 | VString() {} |
| 55 | |
| 56 | char *m_vlcString; |
| 57 | }; |
| 58 | |
| 59 | #endif // PHONON_VLC_VSTRING_H |
| 60 | |