2016-08-16 19:53:10 +03:00
/*
This file is part of Telegram Desktop ,
2018-01-03 13:23:14 +03:00
the official desktop application for the Telegram messaging service .
2016-08-16 19:53:10 +03:00
2018-01-03 13:23:14 +03:00
For license and copyright information please follow this link :
https : //github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-08-16 19:53:10 +03:00
*/
# include "settings/settings_widget.h"
2016-08-17 18:14:08 +03:00
# include "settings/settings_inner_widget.h"
# include "settings/settings_fixed_bar.h"
# include "styles/style_settings.h"
2016-11-04 11:23:50 +03:00
# include "styles/style_window.h"
2016-10-31 15:29:26 +03:00
# include "styles/style_boxes.h"
2018-03-18 12:51:14 +04:00
# include "platform/platform_specific.h"
2016-11-16 13:44:06 +03:00
# include "ui/widgets/scroll_area.h"
2016-12-13 20:07:56 +03:00
# include "ui/widgets/buttons.h"
2018-03-18 12:51:14 +04:00
# include "ui/toast/toast.h"
2016-08-17 18:14:08 +03:00
# include "mainwindow.h"
2016-09-25 22:04:02 +03:00
# include "mainwidget.h"
2017-03-04 13:23:56 +03:00
# include "storage/localstorage.h"
2017-04-06 17:38:10 +03:00
# include "boxes/confirm_box.h"
2017-04-13 11:27:10 +03:00
# include "lang/lang_keys.h"
2017-05-30 20:58:25 +03:00
# include "lang/lang_cloud_manager.h"
2017-02-23 13:59:19 +03:00
# include "messenger.h"
2017-03-01 20:22:37 +03:00
# include "mtproto/mtp_instance.h"
# include "mtproto/dc_options.h"
2017-02-28 17:05:30 +03:00
# include "core/file_utilities.h"
2018-04-27 17:08:04 +04:00
# include "core/update_checker.h"
2017-02-03 23:07:26 +03:00
# include "window/themes/window_theme.h"
# include "window/themes/window_theme_editor.h"
2017-05-08 12:08:24 +03:00
# include "media/media_audio_track.h"
2018-06-02 17:29:21 +03:00
# include "mainwindow.h"
# include "window/window_controller.h"
2016-08-17 18:14:08 +03:00
2016-08-16 19:53:10 +03:00
namespace Settings {
2016-08-27 11:52:05 -06:00
namespace {
QString SecretText ;
2018-06-04 18:35:11 +03:00
QMap < QString , Fn < void ( ) > > Codes ;
2016-08-27 11:52:05 -06:00
void fillCodes ( ) {
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " debugmode " ) , [ ] {
2018-06-05 16:32:26 +03:00
QString text = Logs : : DebugEnabled ( )
? qsl ( " Do you want to disable DEBUG logs? " )
: qsl ( " Do you want to enable DEBUG logs? \n \n "
" All network events will be logged. " ) ;
2016-12-13 20:07:56 +03:00
Ui : : show ( Box < ConfirmBox > ( text , [ ] {
2017-08-04 16:54:32 +02:00
Messenger : : Instance ( ) . onSwitchDebugMode ( ) ;
2016-12-13 20:07:56 +03:00
} ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " testmode " ) , [ ] {
2016-12-13 20:07:56 +03:00
auto text = cTestMode ( ) ? qsl ( " Do you want to disable TEST mode? " ) : qsl ( " Do you want to enable TEST mode? \n \n You will be switched to test cloud. " ) ;
Ui : : show ( Box < ConfirmBox > ( text , [ ] {
2017-08-04 16:54:32 +02:00
Messenger : : Instance ( ) . onSwitchTestMode ( ) ;
2016-12-13 20:07:56 +03:00
} ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2018-05-07 00:34:58 +03:00
# ifndef TDESKTOP_DISABLE_AUTOUPDATE
2018-04-27 17:08:04 +04:00
Codes . insert ( qsl ( " testupdate " ) , [ ] {
Core : : UpdateChecker ( ) . test ( ) ;
} ) ;
2018-05-07 00:34:58 +03:00
# endif // TDESKTOP_DISABLE_AUTOUPDATE
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " loadlang " ) , [ ] {
2017-05-30 20:58:25 +03:00
Lang : : CurrentCloudManager ( ) . switchToLanguage ( qsl ( " custom " ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " debugfiles " ) , [ ] {
2018-06-05 16:32:26 +03:00
if ( ! Logs : : DebugEnabled ( ) ) {
return ;
}
2016-08-27 11:52:05 -06:00
if ( DebugLogging : : FileLoader ( ) ) {
Global : : RefDebugLoggingFlags ( ) & = ~ DebugLogging : : FileLoaderFlag ;
} else {
Global : : RefDebugLoggingFlags ( ) | = DebugLogging : : FileLoaderFlag ;
}
2016-12-13 20:07:56 +03:00
Ui : : show ( Box < InformBox > ( DebugLogging : : FileLoader ( ) ? qsl ( " Enabled file download logging " ) : qsl ( " Disabled file download logging " ) ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " crashplease " ) , [ ] {
2017-03-17 15:05:50 +03:00
Unexpected ( " Crashed in Settings! " ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " workmode " ) , [ ] {
2016-08-27 11:52:05 -06:00
auto text = Global : : DialogsModeEnabled ( ) ? qsl ( " Disable work mode? " ) : qsl ( " Enable work mode? " ) ;
2016-12-13 20:07:56 +03:00
Ui : : show ( Box < ConfirmBox > ( text , [ ] {
2017-08-04 16:54:32 +02:00
Messenger : : Instance ( ) . onSwitchWorkMode ( ) ;
2016-12-13 20:07:56 +03:00
} ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " moderate " ) , [ ] {
2016-08-27 11:52:05 -06:00
auto text = Global : : ModerateModeEnabled ( ) ? qsl ( " Disable moderate mode? " ) : qsl ( " Enable moderate mode? " ) ;
2016-12-13 20:07:56 +03:00
Ui : : show ( Box < ConfirmBox > ( text , [ ] ( ) {
2016-08-27 11:52:05 -06:00
Global : : SetModerateModeEnabled ( ! Global : : ModerateModeEnabled ( ) ) ;
Local : : writeUserSettings ( ) ;
Ui : : hideLayer ( ) ;
2016-12-13 20:07:56 +03:00
} ) ) ;
2016-08-27 11:52:05 -06:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " getdifference " ) , [ ] {
2016-09-25 22:04:02 +03:00
if ( auto main = App : : main ( ) ) {
main - > getDifference ( ) ;
}
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " loadcolors " ) , [ ] {
2018-04-17 19:19:34 +04:00
FileDialog : : GetOpenPath ( Messenger : : Instance ( ) . getFileDialogParent ( ) , " Open palette file " , " Palette (*.tdesktop-palette) " , [ ] ( const FileDialog : : OpenResult & result ) {
2017-02-03 23:07:26 +03:00
if ( ! result . paths . isEmpty ( ) ) {
Window : : Theme : : Apply ( result . paths . front ( ) ) ;
}
} ) ;
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " edittheme " ) , [ ] {
2017-02-07 20:39:47 +03:00
Window : : Theme : : Editor : : Start ( ) ;
2017-02-03 23:07:26 +03:00
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " videoplayer " ) , [ ] {
2017-02-17 23:50:27 +11:00
auto text = cUseExternalVideoPlayer ( ) ? qsl ( " Use internal video player? " ) : qsl ( " Use external video player? " ) ;
Ui : : show ( Box < ConfirmBox > ( text , [ ] {
cSetUseExternalVideoPlayer ( ! cUseExternalVideoPlayer ( ) ) ;
Local : : writeUserSettings ( ) ;
Ui : : hideLayer ( ) ;
} ) ) ;
} ) ;
2017-05-08 12:08:24 +03:00
Codes . insert ( qsl ( " endpoints " ) , [ ] {
2018-04-17 19:19:34 +04:00
FileDialog : : GetOpenPath ( Messenger : : Instance ( ) . getFileDialogParent ( ) , " Open DC endpoints " , " DC Endpoints (*.tdesktop-endpoints) " , [ ] ( const FileDialog : : OpenResult & result ) {
2017-03-01 20:22:37 +03:00
if ( ! result . paths . isEmpty ( ) ) {
if ( ! Messenger : : Instance ( ) . mtp ( ) - > dcOptions ( ) - > loadFromFile ( result . paths . front ( ) ) ) {
Ui : : show ( Box < InformBox > ( " Could not load endpoints :( Errors in 'log.txt'. " ) ) ;
}
}
} ) ;
} ) ;
2018-03-18 12:51:14 +04:00
Codes . insert ( qsl ( " registertg " ) , [ ] {
Platform : : RegisterCustomScheme ( ) ;
Ui : : Toast : : Show ( " Forced custom scheme register. " ) ;
} ) ;
2018-06-02 17:29:21 +03:00
Codes . insert ( qsl ( " export " ) , [ ] {
App : : wnd ( ) - > controller ( ) - > startDataExport ( ) ;
} ) ;
2017-04-13 11:45:58 +03:00
2017-05-08 12:08:24 +03:00
auto audioFilters = qsl ( " Audio files (*.wav *.mp3);; " ) + FileDialog : : AllFilesFilter ( ) ;
auto audioKeys = {
qsl ( " msg_incoming " ) ,
qsl ( " call_incoming " ) ,
qsl ( " call_outgoing " ) ,
qsl ( " call_busy " ) ,
qsl ( " call_connect " ) ,
qsl ( " call_end " ) ,
} ;
for ( auto & key : audioKeys ) {
Codes . insert ( key , [ audioFilters , key ] {
if ( ! AuthSession : : Exists ( ) ) {
return ;
}
2018-04-17 19:19:34 +04:00
FileDialog : : GetOpenPath ( Messenger : : Instance ( ) . getFileDialogParent ( ) , " Open audio file " , audioFilters , [ key ] ( const FileDialog : : OpenResult & result ) {
2017-05-08 12:08:24 +03:00
if ( AuthSession : : Exists ( ) & & ! result . paths . isEmpty ( ) ) {
auto track = Media : : Audio : : Current ( ) . createTrack ( ) ;
track - > fillFromFile ( result . paths . front ( ) ) ;
if ( track - > failed ( ) ) {
Ui : : show ( Box < InformBox > ( " Could not audio :( Errors in 'log.txt'. " ) ) ;
} else {
2018-01-04 13:22:53 +03:00
Auth ( ) . settings ( ) . setSoundOverride ( key , result . paths . front ( ) ) ;
2017-05-08 12:08:24 +03:00
Local : : writeUserSettings ( ) ;
}
}
} ) ;
} ) ;
}
Codes . insert ( qsl ( " sounds_reset " ) , [ ] {
if ( AuthSession : : Exists ( ) ) {
2018-01-04 13:22:53 +03:00
Auth ( ) . settings ( ) . clearSoundOverrides ( ) ;
2017-05-08 12:08:24 +03:00
Local : : writeUserSettings ( ) ;
Ui : : show ( Box < InformBox > ( " All sound overrides were reset. " ) ) ;
}
} ) ;
2016-08-27 11:52:05 -06:00
}
void codesFeedString ( const QString & text ) {
if ( Codes . isEmpty ( ) ) fillCodes ( ) ;
SecretText + = text . toLower ( ) ;
int size = SecretText . size ( ) , from = 0 ;
while ( size > from ) {
auto piece = SecretText . midRef ( from ) ;
auto found = false ;
for ( auto i = Codes . cbegin ( ) , e = Codes . cend ( ) ; i ! = e ; + + i ) {
if ( piece = = i . key ( ) ) {
( * i ) ( ) ;
from = size ;
found = true ;
break ;
}
}
if ( found ) break ;
for ( auto i = Codes . cbegin ( ) , e = Codes . cend ( ) ; i ! = e ; + + i ) {
if ( i . key ( ) . startsWith ( piece ) ) {
found = true ;
break ;
}
}
if ( found ) break ;
+ + from ;
}
SecretText = ( size > from ) ? SecretText . mid ( from ) : QString ( ) ;
}
} // namespace
2016-08-16 19:53:10 +03:00
2017-02-03 23:07:26 +03:00
Widget : : Widget ( QWidget * parent ) {
2017-04-18 20:37:14 +03:00
refreshLang ( ) ;
2017-05-30 16:54:05 +03:00
subscribe ( Lang : : Current ( ) . updated ( ) , [ this ] { refreshLang ( ) ; } ) ;
2017-04-18 20:37:14 +03:00
2017-02-03 23:07:26 +03:00
_inner = setInnerWidget ( object_ptr < InnerWidget > ( this ) ) ;
setCloseClickHandler ( [ ] ( ) {
2016-12-13 20:07:56 +03:00
Ui : : hideSettingsAndLayer ( ) ;
} ) ;
2017-02-03 23:07:26 +03:00
}
2016-08-26 22:49:18 -06:00
2017-04-18 20:37:14 +03:00
void Widget : : refreshLang ( ) {
setTitle ( lang ( lng_menu_settings ) ) ;
update ( ) ;
}
2018-04-18 19:42:02 +04:00
void Widget : : scrollToUpdateRow ( ) {
if ( const auto top = _inner - > getUpdateTop ( ) ; top > = 0 ) {
scrollToY ( top ) ;
}
}
2017-02-03 23:07:26 +03:00
void Widget : : keyPressEvent ( QKeyEvent * e ) {
codesFeedString ( e - > text ( ) ) ;
return LayerWidget : : keyPressEvent ( e ) ;
2016-12-13 20:07:56 +03:00
}
2016-08-17 18:14:08 +03:00
void Widget : : parentResized ( ) {
2016-11-05 11:36:24 +03:00
auto parentSize = parentWidget ( ) - > size ( ) ;
2017-02-03 23:07:26 +03:00
auto windowWidth = parentSize . width ( ) ;
auto newWidth = st : : settingsMaxWidth ;
auto newContentLeft = st : : settingsMaxPadding ;
2016-08-17 18:14:08 +03:00
if ( windowWidth < = st : : settingsMaxWidth ) {
newWidth = windowWidth ;
newContentLeft = st : : settingsMinPadding ;
2016-11-04 11:23:50 +03:00
if ( windowWidth > st : : windowMinWidth ) {
// Width changes from st::windowMinWidth to st::settingsMaxWidth.
2016-08-17 18:14:08 +03:00
// Padding changes from st::settingsMinPadding to st::settingsMaxPadding.
2016-11-04 11:23:50 +03:00
newContentLeft + = ( ( newWidth - st : : windowMinWidth ) * ( st : : settingsMaxPadding - st : : settingsMinPadding ) ) / ( st : : settingsMaxWidth - st : : windowMinWidth ) ;
2016-08-17 18:14:08 +03:00
}
} else if ( windowWidth < st : : settingsMaxWidth + 2 * st : : settingsMargin ) {
newWidth = windowWidth - 2 * st : : settingsMargin ;
newContentLeft = st : : settingsMinPadding ;
2016-11-04 11:23:50 +03:00
if ( windowWidth > st : : windowMinWidth ) {
// Width changes from st::windowMinWidth to st::settingsMaxWidth.
2016-08-17 18:14:08 +03:00
// Padding changes from st::settingsMinPadding to st::settingsMaxPadding.
2016-11-04 11:23:50 +03:00
newContentLeft + = ( ( newWidth - st : : windowMinWidth ) * ( st : : settingsMaxPadding - st : : settingsMinPadding ) ) / ( st : : settingsMaxWidth - st : : windowMinWidth ) ;
2016-08-17 18:14:08 +03:00
}
}
2017-02-03 23:07:26 +03:00
resizeToWidth ( newWidth , newContentLeft ) ;
2016-08-26 22:49:18 -06:00
}
2017-02-03 23:07:26 +03:00
void Widget : : resizeUsingInnerHeight ( int newWidth , int innerHeight ) {
2017-09-13 19:57:44 +03:00
if ( ! parentWidget ( ) ) return ;
2016-08-26 22:49:18 -06:00
2016-11-05 11:36:24 +03:00
auto parentSize = parentWidget ( ) - > size ( ) ;
2017-02-03 23:07:26 +03:00
auto windowWidth = parentSize . width ( ) ;
auto windowHeight = parentSize . height ( ) ;
auto maxHeight = st : : settingsFixedBarHeight + innerHeight ;
auto newHeight = maxHeight + st : : boxRadius ;
2016-08-17 18:14:08 +03:00
if ( newHeight > windowHeight | | newWidth > = windowWidth ) {
newHeight = windowHeight ;
}
2017-02-03 23:07:26 +03:00
auto roundedCorners = newHeight < windowHeight ;
setRoundedCorners ( roundedCorners ) ;
setAttribute ( Qt : : WA_OpaquePaintEvent , ! roundedCorners ) ;
2016-12-13 20:07:56 +03:00
2016-11-05 11:36:24 +03:00
setGeometry ( ( windowWidth - newWidth ) / 2 , ( windowHeight - newHeight ) / 2 , newWidth , newHeight ) ;
2016-08-17 18:14:08 +03:00
update ( ) ;
}
2016-08-16 19:53:10 +03:00
} // namespace Settings