Sayonara Player
Utils.h
1 /* Helper.h */
2 
3 /* Copyright (C) 2011-2017 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 /*
22  * Helper.cpp
23  *
24  * Created on: Apr 4, 2011
25  * Author: Lucio Carreras
26  */
27 
28 #ifndef _HELPER_H
29 #define _HELPER_H
30 
31 class QDateTime;
32 class QPixmap;
33 
34 #include <QObject>
35 #include <algorithm>
36 #include "typedefs.h"
37 
38 #define LOCK_GUARD(locking_mutex) std::lock_guard<std::mutex> g(locking_mutex); Q_UNUSED(g)
39 
44 namespace Util
45 {
46  uint64_t current_date_to_int();
47  uint64_t date_to_int(const QDateTime& date);
48  QDateTime int_to_date(uint64_t date);
49 
55  QString cvt_str_to_first_upper(const QString& str);
56 
62  QString cvt_str_to_very_first_upper(const QString& str);
63 
72  QString cvt_ms_to_string(MilliSeconds msec, bool empty_zero = false, bool colon=true, bool show_days=true);
73 
74  QString cvt_not_null(const QString& str);
75 
76 
81  QString sayonara_path(const QString& append_path=QString());
82 
87  QString share_path(const QString& append_path=QString());
88 
93  QString lib_path(const QString& append_path=QString());
94 
102  QString create_link(const QString& name,
103  bool dark=true,
104  const QString& target="",
105  bool underline=true);
106 
107 
112  QStringList soundfile_extensions(bool with_asterisk=true);
113 
118  QStringList playlist_extensions(bool with_asterisk=true);
119 
124  QStringList podcast_extensions(bool with_asterisk=true);
125 
126  QStringList image_extensions(bool with_asterisk=true);
127 
128 
135  int random_number(int min, int max);
136 
137 
138  QString random_string(int max_chars);
139 
140 
147  QString easy_tag_finder(const QString& tag, const QString& xml_doc);
148 
154  QByteArray calc_hash(const QByteArray& data);
155 
156 
161  void sleep_ms(uint64_t ms);
162 
167  QStringList ip_addresses();
168 
169 
170  QByteArray cvt_pixmap_to_bytearray(const QPixmap& pm);
171  QPixmap cvt_bytearray_to_pixmap(const QByteArray& arr);
172 
178  void set_environment(const QString& key, const QString& value);
179  void unset_environment(const QString& key);
180 
181  template<typename T, typename FN>
182  bool contains(const T& container, FN fn)
183  {
184  return std::any_of(container.begin(), container.end(), fn);
185  }
186 
187  template<typename T, typename FN>
188  void sort(T& container, FN fn)
189  {
190  std::sort(container.begin(), container.end(), fn);
191  }
192 
193  template<typename T, typename FN>
194  typename T::iterator find(T& container, FN fn)
195  {
196  return std::find_if(container.begin(), container.end(), fn);
197  }
198 
199  template<typename T, typename FN>
200  typename T::const_iterator find(const T& container, FN fn)
201  {
202  return std::find_if(container.begin(), container.end(), fn);
203  }
204 
205  template<typename T>
206  constexpr typename std::add_const<T>::type& AsConst(T& t) {
207  return t;
208  }
209 
210  template<typename T, typename FN>
211  int indexOf(const T& container, FN fn) {
212  auto it = Util::find(container, fn);
213  if(it == container.end())
214  {
215  return -1;
216  }
217  return std::distance(container.begin(), it);
218  }
219 }
220 
221 #endif
QStringList playlist_extensions(bool with_asterisk=true)
get all supported playlist file extensions
void set_environment(const QString &key, const QString &value)
set an environment variable. This function is platform independent
void sleep_ms(uint64_t ms)
sleep
QStringList soundfile_extensions(bool with_asterisk=true)
get all supported sound file extensions
QString cvt_str_to_very_first_upper(const QString &str)
Transform only first letter to upper case.
QStringList ip_addresses()
get all ip addresses of the host
QByteArray calc_hash(const QByteArray &data)
calculate a md5 hashsum
QString share_path(const QString &append_path=QString())
get share path of sayonara
QString cvt_str_to_first_upper(const QString &str)
Transform all letters after a space to upper case.
QString lib_path(const QString &append_path=QString())
get library path of sayonara
int random_number(int min, int max)
get a random val between min max
Helper functions.
Definition: GenreView.h:34
QString sayonara_path(const QString &append_path=QString())
get sayonara path in home directory
QString create_link(const QString &name, bool dark=true, const QString &target="", bool underline=true)
create a link string
QString cvt_ms_to_string(MilliSeconds msec, bool empty_zero=false, bool colon=true, bool show_days=true)
Convert milliseconds to string.
QString easy_tag_finder(const QString &tag, const QString &xml_doc)
gets value out of tag
QStringList podcast_extensions(bool with_asterisk=true)
get all supported podcast file extensions