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 
54  QString cvt_str_to_first_upper(const QString& str);
55 
61  QString cvt_str_to_very_first_upper(const QString& str);
62 
71  QString cvt_ms_to_string(MilliSeconds msec, bool empty_zero = false, bool colon=true, bool show_days=true);
72 
73  QString cvt_not_null(const QString& str);
74 
75 
80  QString sayonara_path(const QString& append_path=QString());
81 
86  QString share_path(const QString& append_path=QString());
87 
92  QString lib_path(const QString& append_path=QString());
93 
101  QString create_link(const QString& name,
102  bool dark=true,
103  const QString& target="",
104  bool underline=true);
105 
106 
111  QStringList soundfile_extensions(bool with_asterisk=true);
112 
117  QStringList playlist_extensions(bool with_asterisk=true);
118 
123  QStringList podcast_extensions(bool with_asterisk=true);
124 
125  QStringList image_extensions(bool with_asterisk=true);
126 
127 
134  int random_number(int min, int max);
135 
136 
137  QString random_string(int max_chars);
138 
139 
146  QString easy_tag_finder(const QString& tag, const QString& xml_doc);
147 
153  QByteArray calc_hash(const QByteArray& data);
154 
155 
160  void sleep_ms(uint64_t ms);
161 
166  QStringList ip_addresses();
167 
168 
169  QByteArray cvt_pixmap_to_bytearray(const QPixmap& pm);
170  QPixmap cvt_bytearray_to_pixmap(const QByteArray& arr);
171 
177  void set_environment(const QString& key, const QString& value);
178  void unset_environment(const QString& key);
179 
180  template<typename T, typename FN>
181  bool contains(const T& container, FN fn)
182  {
183  return std::any_of(container.begin(), container.end(), fn);
184  }
185 
186  template<typename T, typename FN>
187  void sort(T& container, FN fn)
188  {
189  std::sort(container.begin(), container.end(), fn);
190  }
191 
192  template<typename T, typename FN>
193  typename T::iterator find(T& container, FN fn)
194  {
195  return std::find_if(container.begin(), container.end(), fn);
196  }
197 
198  template<typename T, typename FN>
199  typename T::const_iterator find(const T& container, FN fn)
200  {
201  return std::find_if(container.begin(), container.end(), fn);
202  }
203 
204  template<typename T>
205  constexpr typename std::add_const<T>::type& AsConst(T& t) {
206  return t;
207  }
208 
209  template<typename T, typename FN>
210  int indexOf(const T& container, FN fn) {
211  auto it = Util::find(container, fn);
212  if(it == container.end())
213  {
214  return -1;
215  }
216  return std::distance(container.begin(), it);
217  }
218 }
219 
220 #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: Crypt.h:29
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