Sayonara Player
Utils.h
1 /* Helper.h */
2 
3 /* Copyright (C) 2011-2019 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 UTIL_HELPER_H
29 #define UTIL_HELPER_H
30 
31 class QDateTime;
32 class QPixmap;
33 
34 #include <QObject>
35 #include <algorithm>
36 #include "typedefs.h"
37 
38 #ifndef CAST_MACROS
39  #define scast(x, y) static_cast<x>(y)
40  #define dcast(x, y) dynamic_cast<x>(y)
41  #define rcast(x, y) reinterpret_cast<x>(y)
42  #define CAST_MACROS
43 #endif
44 
45 #define LOCK_GUARD(locking_mutex) std::lock_guard<std::mutex> g(locking_mutex); Q_UNUSED(g)
46 
51 namespace Util
52 {
53  uint64_t current_date_to_int();
54  uint64_t date_to_int(const QDateTime& date);
55  QDateTime int_to_date(uint64_t date);
56 
62  QString cvt_str_to_first_upper(const QString& str);
63 
69  QString cvt_str_to_very_first_upper(const QString& str);
70 
79  QString cvt_ms_to_string(MilliSeconds msec, bool empty_zero = false, bool colon=true, bool show_days=true);
80 
81  QString cvt_not_null(const QString& str);
82 
83 
88  QString sayonara_path(const QString& append_path=QString());
89 
94  QString share_path(const QString& append_path=QString());
95 
100  QString lib_path(const QString& append_path=QString());
101 
109  QString create_link(const QString& name,
110  bool dark=true,
111  const QString& target="",
112  bool underline=true);
113 
114 
119  QStringList soundfile_extensions(bool with_asterisk=true);
120 
125  QStringList playlist_extensions(bool with_asterisk=true);
126 
131  QStringList podcast_extensions(bool with_asterisk=true);
132 
133  QStringList image_extensions(bool with_asterisk=true);
134 
135 
142  int random_number(int min, int max);
143 
144 
145  QString random_string(int max_chars);
146 
147 
154  QString easy_tag_finder(const QString& tag, const QString& xml_doc);
155 
161  QByteArray calc_hash(const QByteArray& data);
162 
163 
168  void sleep_ms(uint64_t ms);
169 
174  QStringList ip_addresses();
175 
176 
177  QByteArray cvt_pixmap_to_bytearray(const QPixmap& pm);
178  QPixmap cvt_bytearray_to_pixmap(const QByteArray& arr);
179 
185  void set_environment(const QString& key, const QString& value);
186  void unset_environment(const QString& key);
187 
188  template<typename T, typename FN>
189  bool contains(const T& container, FN fn)
190  {
191  return std::any_of(container.begin(), container.end(), fn);
192  }
193 
194  template<typename T, typename FN>
195  void sort(T& container, FN fn)
196  {
197  std::sort(container.begin(), container.end(), fn);
198  }
199 
200  template<typename T, typename FN>
201  typename T::iterator find(T& container, FN fn)
202  {
203  return std::find_if(container.begin(), container.end(), fn);
204  }
205 
206  template<typename T, typename FN>
207  typename T::const_iterator find(const T& container, FN fn)
208  {
209  return std::find_if(container.begin(), container.end(), fn);
210  }
211 
212  template<typename T>
213  constexpr typename std::add_const<T>::type& AsConst(T& t) {
214  return t;
215  }
216 
217  template<typename T, typename FN>
218  int indexOf(const T& container, FN fn) {
219  auto it = Util::find(container, fn);
220  if(it == container.end())
221  {
222  return -1;
223  }
224  return std::distance(container.begin(), it);
225  }
226 }
227 
228 #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:35
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