2020-04-03 22:28:15 +00:00
/*
* Squawk messenger .
* Copyright ( C ) 2019 Yury Gubich < blue @ macaw . me >
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "global.h"
# include "enums.h"
2022-04-18 16:54:42 +00:00
# include "ui/models/roster.h"
2020-04-03 22:28:15 +00:00
Shared : : Global * Shared : : Global : : instance = 0 ;
2020-04-13 19:57:23 +00:00
const std : : set < QString > Shared : : Global : : supportedImagesExts = { " png " , " jpg " , " webp " , " jpeg " , " gif " , " svg " } ;
2020-04-03 22:28:15 +00:00
2021-05-06 14:44:43 +00:00
# ifdef WITH_KIO
QLibrary Shared : : Global : : openFileManagerWindowJob ( " openFileManagerWindowJob " ) ;
Shared : : Global : : HighlightInFileManager Shared : : Global : : hfm = 0 ;
# endif
2022-01-26 20:53:44 +00:00
# ifdef WITH_KCONFIG
QLibrary Shared : : Global : : colorSchemeTools ( " colorSchemeTools " ) ;
Shared : : Global : : CreatePreview Shared : : Global : : createPreview = 0 ;
Shared : : Global : : DeletePreview Shared : : Global : : deletePreview = 0 ;
2022-01-27 17:44:32 +00:00
Shared : : Global : : ColorSchemeName Shared : : Global : : colorSchemeName = 0 ;
Shared : : Global : : CreatePalette Shared : : Global : : createPalette = 0 ;
2022-01-26 20:53:44 +00:00
# endif
2020-04-03 22:28:15 +00:00
Shared : : Global : : Global ( ) :
availability ( {
2020-04-10 22:15:08 +00:00
tr ( " Online " , " Availability " ) ,
tr ( " Away " , " Availability " ) ,
tr ( " Absent " , " Availability " ) ,
tr ( " Busy " , " Availability " ) ,
tr ( " Chatty " , " Availability " ) ,
tr ( " Invisible " , " Availability " ) ,
tr ( " Offline " , " Availability " )
2020-04-03 22:28:15 +00:00
} ) ,
connectionState ( {
2020-04-10 22:15:08 +00:00
tr ( " Disconnected " , " ConnectionState " ) ,
2022-12-26 22:01:01 +00:00
tr ( " Scheduled " , " ConnectionState " ) ,
tr ( " Connecting " , " ConnectionState " ) ,
2020-04-10 22:15:08 +00:00
tr ( " Connected " , " ConnectionState " ) ,
tr ( " Error " , " ConnectionState " )
2020-04-03 22:28:15 +00:00
} ) ,
subscriptionState ( {
2020-04-10 22:15:08 +00:00
tr ( " None " , " SubscriptionState " ) ,
tr ( " From " , " SubscriptionState " ) ,
tr ( " To " , " SubscriptionState " ) ,
tr ( " Both " , " SubscriptionState " ) ,
tr ( " Unknown " , " SubscriptionState " )
2020-04-03 22:28:15 +00:00
} ) ,
affiliation ( {
2020-04-10 22:15:08 +00:00
tr ( " Unspecified " , " Affiliation " ) ,
tr ( " Outcast " , " Affiliation " ) ,
tr ( " Nobody " , " Affiliation " ) ,
tr ( " Member " , " Affiliation " ) ,
tr ( " Admin " , " Affiliation " ) ,
tr ( " Owner " , " Affiliation " )
2020-04-03 22:28:15 +00:00
} ) ,
role ( {
2020-04-10 22:15:08 +00:00
tr ( " Unspecified " , " Role " ) ,
tr ( " Nobody " , " Role " ) ,
tr ( " Visitor " , " Role " ) ,
tr ( " Participant " , " Role " ) ,
tr ( " Moderator " , " Role " )
2020-04-03 22:28:15 +00:00
} ) ,
messageState ( {
2020-04-10 22:15:08 +00:00
tr ( " Pending " , " MessageState " ) ,
tr ( " Sent " , " MessageState " ) ,
tr ( " Delivered " , " MessageState " ) ,
tr ( " Error " , " MessageState " )
2020-04-04 16:40:32 +00:00
} ) ,
accountPassword ( {
2020-04-10 22:15:08 +00:00
tr ( " Plain " , " AccountPassword " ) ,
tr ( " Jammed " , " AccountPassword " ) ,
tr ( " Always Ask " , " AccountPassword " ) ,
tr ( " KWallet " , " AccountPassword " )
} ) ,
accountPasswordDescription ( {
tr ( " Your password is going to be stored in config file in plain text " , " AccountPasswordDescription " ) ,
tr ( " Your password is going to be stored in config file but jammed with constant encryption key you can find in program source code. It might look like encryption but it's not " , " AccountPasswordDescription " ) ,
tr ( " Squawk is going to query you for the password on every start of the program " , " AccountPasswordDescription " ) ,
tr ( " Your password is going to be stored in KDE wallet storage (KWallet). You're going to be queried for permissions " , " AccountPasswordDescription " )
} ) ,
2022-01-21 19:02:50 +00:00
defaultSystemStyle ( QApplication : : style ( ) - > objectName ( ) ) ,
2022-01-27 17:44:32 +00:00
defaultSystemPalette ( QApplication : : palette ( ) ) ,
2020-04-10 22:15:08 +00:00
pluginSupport ( {
2021-05-06 14:44:43 +00:00
{ " KWallet " , false } ,
2022-01-26 20:53:44 +00:00
{ " openFileManagerWindowJob " , false } ,
{ " colorSchemeTools " , false }
2021-04-20 21:56:47 +00:00
} ) ,
fileCache ( )
2020-04-03 22:28:15 +00:00
{
if ( instance ! = 0 ) {
throw 551 ;
}
instance = this ;
2021-09-21 22:17:43 +00:00
2021-05-06 14:44:43 +00:00
# ifdef WITH_KIO
openFileManagerWindowJob . load ( ) ;
if ( openFileManagerWindowJob . isLoaded ( ) ) {
hfm = ( HighlightInFileManager ) openFileManagerWindowJob . resolve ( " highlightInFileManager " ) ;
if ( hfm ) {
setSupported ( " openFileManagerWindowJob " , true ) ;
qDebug ( ) < < " KIO::OpenFileManagerWindow support enabled " ;
} else {
qDebug ( ) < < " KIO::OpenFileManagerWindow support disabled: couldn't resolve required methods in the library " ;
}
} else {
qDebug ( ) < < " KIO::OpenFileManagerWindow support disabled: couldn't load the library " < < openFileManagerWindowJob . errorString ( ) ;
}
# endif
2022-01-26 20:53:44 +00:00
# ifdef WITH_KCONFIG
colorSchemeTools . load ( ) ;
if ( colorSchemeTools . isLoaded ( ) ) {
createPreview = ( CreatePreview ) colorSchemeTools . resolve ( " createPreview " ) ;
deletePreview = ( DeletePreview ) colorSchemeTools . resolve ( " deletePreview " ) ;
2022-01-27 17:44:32 +00:00
colorSchemeName = ( ColorSchemeName ) colorSchemeTools . resolve ( " colorSchemeName " ) ;
createPalette = ( CreatePalette ) colorSchemeTools . resolve ( " createPalette " ) ;
if ( createPreview & & deletePreview & & colorSchemeName & & createPalette ) {
2022-01-26 20:53:44 +00:00
setSupported ( " colorSchemeTools " , true ) ;
qDebug ( ) < < " Color Schemes support enabled " ;
} else {
qDebug ( ) < < " Color Schemes support disabled: couldn't resolve required methods in the library " ;
}
} else {
qDebug ( ) < < " Color Schemes support disabled: couldn't load the library " < < colorSchemeTools . errorString ( ) ;
}
# endif
2020-04-03 22:28:15 +00:00
}
2021-05-10 21:06:40 +00:00
static const QSize defaultIconFileInfoHeight ( 50 , 50 ) ;
2021-04-20 21:56:47 +00:00
Shared : : Global : : FileInfo Shared : : Global : : getFileInfo ( const QString & path )
{
std : : map < QString , FileInfo > : : const_iterator itr = instance - > fileCache . find ( path ) ;
if ( itr = = instance - > fileCache . end ( ) ) {
QMimeDatabase db ;
QMimeType type = db . mimeTypeForFile ( path ) ;
QStringList parts = type . name ( ) . split ( " / " ) ;
QString big = parts . front ( ) ;
QFileInfo info ( path ) ;
FileInfo : : Preview p = FileInfo : : Preview : : none ;
QSize size ;
if ( big = = " image " ) {
2021-05-15 22:07:49 +00:00
QMovie mov ( path ) ;
2021-05-17 20:32:44 +00:00
if ( mov . isValid ( ) & & mov . frameCount ( ) > 1 ) {
2021-05-15 22:07:49 +00:00
p = FileInfo : : Preview : : animation ;
} else {
p = FileInfo : : Preview : : picture ;
2021-04-20 21:56:47 +00:00
}
2021-05-17 20:32:44 +00:00
QImageReader img ( path ) ;
2021-04-20 21:56:47 +00:00
size = img . size ( ) ;
2021-05-15 22:07:49 +00:00
// } else if (big == "video") {
// p = FileInfo::Preview::movie;
// QMovie mov(path);
// size = mov.scaledSize();
// qDebug() << mov.isValid();
2021-05-10 21:06:40 +00:00
} else {
size = defaultIconFileInfoHeight ;
2021-04-20 21:56:47 +00:00
}
2022-01-09 14:32:23 +00:00
itr = instance - > fileCache . insert ( std : : make_pair ( path , FileInfo ( { info . absoluteFilePath ( ) , info . fileName ( ) , size , type , p } ) ) ) . first ;
2021-04-20 21:56:47 +00:00
}
return itr - > second ;
}
2020-04-03 22:28:15 +00:00
Shared : : Global * Shared : : Global : : getInstance ( )
{
return instance ;
}
QString Shared : : Global : : getName ( Message : : State rl )
{
2020-04-04 16:40:32 +00:00
return instance - > messageState [ static_cast < int > ( rl ) ] ;
2020-04-03 22:28:15 +00:00
}
QString Shared : : Global : : getName ( Shared : : Affiliation af )
{
2020-04-04 16:40:32 +00:00
return instance - > affiliation [ static_cast < int > ( af ) ] ;
2020-04-03 22:28:15 +00:00
}
QString Shared : : Global : : getName ( Shared : : Availability av )
{
2020-04-04 16:40:32 +00:00
return instance - > availability [ static_cast < int > ( av ) ] ;
2020-04-03 22:28:15 +00:00
}
QString Shared : : Global : : getName ( Shared : : ConnectionState cs )
{
2020-04-04 16:40:32 +00:00
return instance - > connectionState [ static_cast < int > ( cs ) ] ;
2020-04-03 22:28:15 +00:00
}
QString Shared : : Global : : getName ( Shared : : Role rl )
{
2020-04-04 16:40:32 +00:00
return instance - > role [ static_cast < int > ( rl ) ] ;
2020-04-03 22:28:15 +00:00
}
QString Shared : : Global : : getName ( Shared : : SubscriptionState ss )
{
2020-04-04 16:40:32 +00:00
return instance - > subscriptionState [ static_cast < int > ( ss ) ] ;
2020-04-03 22:28:15 +00:00
}
2020-04-04 16:40:32 +00:00
QString Shared : : Global : : getName ( Shared : : AccountPassword ap )
2020-04-03 22:28:15 +00:00
{
2020-04-04 16:40:32 +00:00
return instance - > accountPassword [ static_cast < int > ( ap ) ] ;
2020-04-03 22:28:15 +00:00
}
2020-04-10 22:15:08 +00:00
void Shared : : Global : : setSupported ( const QString & pluginName , bool support )
{
std : : map < QString , bool > : : iterator itr = instance - > pluginSupport . find ( pluginName ) ;
if ( itr ! = instance - > pluginSupport . end ( ) ) {
itr - > second = support ;
}
}
bool Shared : : Global : : supported ( const QString & pluginName )
{
std : : map < QString , bool > : : iterator itr = instance - > pluginSupport . find ( pluginName ) ;
if ( itr ! = instance - > pluginSupport . end ( ) ) {
return itr - > second ;
}
return false ;
}
QString Shared : : Global : : getDescription ( Shared : : AccountPassword ap )
{
return instance - > accountPasswordDescription [ static_cast < int > ( ap ) ] ;
}
2021-05-06 14:44:43 +00:00
static const QStringList query = { " query " , " default " , " inode/directory " } ;
static const QRegularExpression dolphinReg ( " [Dd]olphin " ) ;
static const QRegularExpression nautilusReg ( " [Nn]autilus " ) ;
static const QRegularExpression cajaReg ( " [Cc]aja " ) ;
static const QRegularExpression nemoReg ( " [Nn]emo " ) ;
static const QRegularExpression konquerorReg ( " kfmclient " ) ;
static const QRegularExpression pcmanfmQtReg ( " pcmanfm-qt " ) ;
static const QRegularExpression pcmanfmReg ( " pcmanfm " ) ;
static const QRegularExpression thunarReg ( " thunar " ) ;
void Shared : : Global : : highlightInFileManager ( const QString & path )
{
# ifdef WITH_KIO
if ( supported ( " openFileManagerWindowJob " ) ) {
hfm ( path ) ;
return ;
} else {
qDebug ( ) < < " requested to highlight in file manager url " < < path < < " but it's not supported: KIO plugin isn't loaded, trying fallback " ;
}
# else
qDebug ( ) < < " requested to highlight in file manager url " < < path < < " but it's not supported: squawk wasn't compiled to support it, trying fallback " ;
# endif
QFileInfo info = path ;
if ( info . exists ( ) ) {
QProcess proc ;
proc . start ( " xdg-mime " , query ) ;
proc . waitForFinished ( ) ;
QString output = proc . readLine ( ) . simplified ( ) ;
QString folder ;
if ( info . isDir ( ) ) {
folder = info . canonicalFilePath ( ) ;
} else {
folder = info . canonicalPath ( ) ;
}
if ( output . contains ( dolphinReg ) ) {
//there is a bug on current (21.04.0) dolphin, it works correct only if you already have dolphin launched
proc . startDetached ( " dolphin " , QStringList ( ) < < " --select " < < info . canonicalFilePath ( ) ) ;
//KIO::highlightInFileManager({QUrl(info.canonicalFilePath())});
} else if ( output . contains ( nautilusReg ) ) {
proc . startDetached ( " nautilus " , QStringList ( ) < < " --select " < < info . canonicalFilePath ( ) ) ; //this worked on nautilus
} else if ( output . contains ( cajaReg ) ) {
proc . startDetached ( " caja " , QStringList ( ) < < folder ) ; //caja doesn't seem to support file selection command line, gonna just open directory
} else if ( output . contains ( nemoReg ) ) {
proc . startDetached ( " nemo " , QStringList ( ) < < info . canonicalFilePath ( ) ) ; //nemo supports selecting files without keys
} else if ( output . contains ( konquerorReg ) ) {
proc . startDetached ( " konqueror " , QStringList ( ) < < " --select " < < info . canonicalFilePath ( ) ) ; //this worked on konqueror
} else if ( output . contains ( pcmanfmQtReg ) ) {
proc . startDetached ( " pcmanfm-qt " , QStringList ( ) < < folder ) ; //pcmanfm-qt doesn't seem to support open with selection, gonna just open directory
} else if ( output . contains ( pcmanfmReg ) ) {
proc . startDetached ( " pcmanfm " , QStringList ( ) < < folder ) ; //pcmanfm also doesn't seem to support open with selection, gonna just open directory
} else if ( output . contains ( thunarReg ) ) {
proc . startDetached ( " thunar " , QStringList ( ) < < folder ) ; //thunar doesn't seem to support open with selection, gonna just open directory
} else {
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( folder ) ) ;
}
}
}
2022-01-26 20:53:44 +00:00
QIcon Shared : : Global : : createThemePreview ( const QString & path )
{
if ( supported ( " colorSchemeTools " ) ) {
QIcon * icon = createPreview ( path ) ;
QIcon localIcon = * icon ;
deletePreview ( icon ) ;
return localIcon ;
} else {
return QIcon ( ) ;
}
}
2022-01-27 17:44:32 +00:00
QString Shared : : Global : : getColorSchemeName ( const QString & path )
{
if ( supported ( " colorSchemeTools " ) ) {
QString res ;
colorSchemeName ( path , res ) ;
return res ;
} else {
return " " ;
}
}
void Shared : : Global : : setTheme ( const QString & path )
{
if ( supported ( " colorSchemeTools " ) ) {
if ( path . toLower ( ) = = " system " ) {
QApplication : : setPalette ( getInstance ( ) - > defaultSystemPalette ) ;
} else {
QPalette pallete ;
createPalette ( path , pallete ) ;
QApplication : : setPalette ( pallete ) ;
}
}
}
void Shared : : Global : : setStyle ( const QString & style )
{
if ( style . toLower ( ) = = " system " ) {
QApplication : : setStyle ( getInstance ( ) - > defaultSystemStyle ) ;
} else {
QApplication : : setStyle ( style ) ;
}
}
2021-05-06 14:44:43 +00:00
2020-04-04 16:40:32 +00:00
# define FROM_INT_INPL(Enum) \
template < > \
Enum Shared : : Global : : fromInt ( int src ) \
{ \
if ( src < static_cast < int > ( Enum # # Lowest ) & & src > static_cast < int > ( Enum # # Highest ) ) { \
throw EnumOutOfRange ( # Enum ) ; \
} \
return static_cast < Enum > ( src ) ; \
} \
template < > \
Enum Shared : : Global : : fromInt ( unsigned int src ) { return fromInt < Enum > ( static_cast < int > ( src ) ) ; }
FROM_INT_INPL ( Shared : : Message : : State )
FROM_INT_INPL ( Shared : : Affiliation )
FROM_INT_INPL ( Shared : : ConnectionState )
FROM_INT_INPL ( Shared : : Role )
FROM_INT_INPL ( Shared : : SubscriptionState )
FROM_INT_INPL ( Shared : : AccountPassword )
FROM_INT_INPL ( Shared : : Avatar )
FROM_INT_INPL ( Shared : : Availability )