2018-06-26 14:09:39 +02:00
# include "ModerationPage.hpp"
# include "Application.hpp"
2018-07-03 14:59:18 +02:00
# include "controllers/moderationactions/ModerationActionModel.hpp"
# include "controllers/moderationactions/ModerationActions.hpp"
2018-06-26 14:09:39 +02:00
# include "controllers/taggedusers/TaggedUsersController.hpp"
# include "controllers/taggedusers/TaggedUsersModel.hpp"
2018-06-28 19:46:45 +02:00
# include "singletons/Logging.hpp"
# include "singletons/Paths.hpp"
2018-08-19 16:18:07 +02:00
# include "util/Helpers.hpp"
2018-06-26 14:09:39 +02:00
# include "util/LayoutCreator.hpp"
# include "widgets/helper/EditableModelView.hpp"
2018-04-27 22:11:19 +02:00
2018-07-03 14:59:18 +02:00
# include <QFileDialog>
2018-05-26 20:25:00 +02:00
# include <QFormLayout>
2018-01-24 15:08:22 +01:00
# include <QGroupBox>
2018-01-13 03:06:10 +01:00
# include <QHBoxLayout>
2018-05-26 20:25:00 +02:00
# include <QHeaderView>
2018-01-13 03:06:10 +01:00
# include <QLabel>
# include <QListView>
# include <QPushButton>
2018-05-26 20:25:00 +02:00
# include <QTableView>
2018-01-13 03:06:10 +01:00
# include <QTextEdit>
# include <QVBoxLayout>
2018-07-11 14:23:21 +02:00
# include <QtConcurrent/QtConcurrent>
2018-01-13 03:06:10 +01:00
2018-01-12 23:09:05 +01:00
namespace chatterino {
2018-03-24 16:55:28 +01:00
2018-07-11 14:23:21 +02:00
qint64 dirSize ( QString dirPath )
{
qint64 size = 0 ;
QDir dir ( dirPath ) ;
// calculate total size of current directories' files
QDir : : Filters fileFilters = QDir : : Files | QDir : : System | QDir : : Hidden ;
for ( QString filePath : dir . entryList ( fileFilters ) ) {
QFileInfo fi ( dir , filePath ) ;
size + = fi . size ( ) ;
}
// add size of child directories recursively
2018-08-06 21:17:03 +02:00
QDir : : Filters dirFilters =
QDir : : Dirs | QDir : : NoDotAndDotDot | QDir : : System | QDir : : Hidden ;
2018-07-11 14:23:21 +02:00
for ( QString childDirPath : dir . entryList ( dirFilters ) )
size + = dirSize ( dirPath + QDir : : separator ( ) + childDirPath ) ;
return size ;
}
QString formatSize ( qint64 size )
{
QStringList units = { " Bytes " , " KB " , " MB " , " GB " , " TB " , " PB " } ;
int i ;
double outputSize = size ;
for ( i = 0 ; i < units . size ( ) - 1 ; i + + ) {
2018-08-06 21:17:03 +02:00
if ( outputSize < 1024 ) break ;
2018-07-11 14:23:21 +02:00
outputSize = outputSize / 1024 ;
}
return QString ( " %0 %1 " ) . arg ( outputSize , 0 , ' f ' , 2 ) . arg ( units [ i ] ) ;
}
QString fetchLogDirectorySize ( )
{
QString logPathDirectory ;
2018-08-12 12:56:28 +02:00
if ( getSettings ( ) - > logPath = = " " ) {
logPathDirectory = getPaths ( ) - > messageLogDirectory ;
2018-07-11 14:23:21 +02:00
} else {
2018-08-12 12:56:28 +02:00
logPathDirectory = getSettings ( ) - > logPath ;
2018-07-11 14:23:21 +02:00
}
qint64 logsSize = dirSize ( logPathDirectory ) ;
QString logsSizeLabel = " Your logs currently take up " ;
logsSizeLabel + = formatSize ( logsSize ) ;
logsSizeLabel + = " of space " ;
return logsSizeLabel ;
}
2018-01-12 23:09:05 +01:00
ModerationPage : : ModerationPage ( )
2018-10-20 00:53:19 +02:00
: SettingsPage ( " Moderation " , " :/settings/moderation.svg " )
2018-01-12 23:09:05 +01:00
{
2018-04-27 22:11:19 +02:00
auto app = getApp ( ) ;
2018-06-26 17:06:17 +02:00
LayoutCreator < ModerationPage > layoutCreator ( this ) ;
2018-01-13 03:06:10 +01:00
2018-05-26 20:25:00 +02:00
auto tabs = layoutCreator . emplace < QTabWidget > ( ) ;
auto logs = tabs . appendTab ( new QVBoxLayout , " Logs " ) ;
2018-01-13 03:06:10 +01:00
{
2018-07-05 15:57:08 +02:00
auto logsPathLabel = logs . emplace < QLabel > ( ) ;
2018-07-11 14:23:21 +02:00
// Show how big (size-wise) the logs are
auto logsPathSizeLabel = logs . emplace < QLabel > ( ) ;
2018-08-06 21:17:03 +02:00
logsPathSizeLabel - > setText (
QtConcurrent : : run ( [ ] { return fetchLogDirectorySize ( ) ; } ) ) ;
2018-07-05 15:57:08 +02:00
2018-07-11 14:23:21 +02:00
// Logs (copied from LoggingMananger)
2018-08-12 12:56:28 +02:00
getSettings ( ) - > logPath . connect (
2018-09-30 19:15:17 +02:00
[ logsPathLabel ] ( const QString & logPath , auto ) mutable {
2018-08-06 21:17:03 +02:00
QString pathOriginal ;
if ( logPath = = " " ) {
2018-08-12 12:56:28 +02:00
pathOriginal = getPaths ( ) - > messageLogDirectory ;
2018-08-06 21:17:03 +02:00
} else {
pathOriginal = logPath ;
}
2018-08-19 16:18:07 +02:00
QString pathShortened =
" Logs saved at <a href= \" file:/// " + pathOriginal +
" \" ><span style= \" color: white; \" > " +
shortenString ( pathOriginal , 50 ) + " </span></a> " ;
2018-08-06 21:17:03 +02:00
logsPathLabel - > setText ( pathShortened ) ;
logsPathLabel - > setToolTip ( pathOriginal ) ;
} ) ;
2018-06-06 20:30:11 +02:00
2018-07-05 15:57:08 +02:00
logsPathLabel - > setTextFormat ( Qt : : RichText ) ;
logsPathLabel - > setTextInteractionFlags ( Qt : : TextBrowserInteraction |
Qt : : LinksAccessibleByKeyboard |
Qt : : LinksAccessibleByKeyboard ) ;
logsPathLabel - > setOpenExternalLinks ( true ) ;
2018-08-06 21:17:03 +02:00
logs . append ( this - > createCheckBox ( " Enable logging " ,
2018-08-12 12:56:28 +02:00
getSettings ( ) - > enableLogging ) ) ;
2018-04-26 23:07:02 +02:00
2018-05-26 20:25:00 +02:00
logs - > addStretch ( 1 ) ;
2018-06-06 20:30:11 +02:00
auto selectDir = logs . emplace < QPushButton > ( " Set custom logpath " ) ;
// Setting custom logpath
2018-08-06 21:17:03 +02:00
QObject : : connect (
selectDir . getElement ( ) , & QPushButton : : clicked , this ,
[ this , logsPathSizeLabel ] ( ) mutable {
auto dirName = QFileDialog : : getExistingDirectory ( this ) ;
2018-06-06 20:30:11 +02:00
2018-08-12 12:56:28 +02:00
getSettings ( ) - > logPath = dirName ;
2018-07-11 14:23:21 +02:00
2018-08-06 21:17:03 +02:00
// Refresh: Show how big (size-wise) the logs are
logsPathSizeLabel - > setText (
QtConcurrent : : run ( [ ] { return fetchLogDirectorySize ( ) ; } ) ) ;
} ) ;
2018-06-06 20:30:11 +02:00
// Reset custom logpath
auto resetDir = logs . emplace < QPushButton > ( " Reset logpath " ) ;
2018-07-11 14:23:21 +02:00
QObject : : connect ( resetDir . getElement ( ) , & QPushButton : : clicked , this ,
[ logsPathSizeLabel ] ( ) mutable {
2018-08-12 12:56:28 +02:00
getSettings ( ) - > logPath = " " ;
2018-07-11 14:23:21 +02:00
// Refresh: Show how big (size-wise) the logs are
2018-08-06 21:17:03 +02:00
logsPathSizeLabel - > setText ( QtConcurrent : : run (
[ ] { return fetchLogDirectorySize ( ) ; } ) ) ;
2018-07-11 14:23:21 +02:00
} ) ;
2018-06-06 20:30:11 +02:00
// Logs end
2018-05-26 20:25:00 +02:00
}
2018-04-26 23:07:02 +02:00
2018-07-03 14:59:18 +02:00
auto modMode = tabs . appendTab ( new QVBoxLayout , " Moderation buttons " ) ;
2018-05-26 20:25:00 +02:00
{
2018-01-13 03:06:10 +01:00
// clang-format off
2018-09-04 22:29:21 +02:00
auto label = modMode . emplace < QLabel > ( " Click the moderation mod button (<img width='18' height='18' src=':/buttons/modModeDisabled.png'>) in a channel that you moderate to enable moderator mode.<br> " ) ;
2018-01-13 03:06:10 +01:00
label - > setWordWrap ( true ) ;
2018-01-24 15:08:22 +01:00
label - > setStyleSheet ( " color: #bbb " ) ;
2018-01-13 03:06:10 +01:00
// clang-format on
2018-05-26 20:25:00 +02:00
// auto form = modMode.emplace<QFormLayout>();
// {
2018-08-06 21:17:03 +02:00
// form->addRow("Action on timed out messages
// (unimplemented):",
2018-05-26 20:25:00 +02:00
// this->createComboBox({"Disable", "Hide"},
2018-08-12 12:56:28 +02:00
// getSettings()->timeoutAction));
2018-05-26 20:25:00 +02:00
// }
2018-03-24 16:55:28 +01:00
2018-07-03 14:59:18 +02:00
EditableModelView * view =
2018-08-06 21:17:03 +02:00
modMode
. emplace < EditableModelView > (
app - > moderationActions - > createModel ( nullptr ) )
2018-07-03 14:59:18 +02:00
. getElement ( ) ;
2018-06-28 19:38:57 +02:00
2018-07-03 14:59:18 +02:00
view - > setTitles ( { " Actions " } ) ;
2018-08-06 21:17:03 +02:00
view - > getTableView ( ) - > horizontalHeader ( ) - > setSectionResizeMode (
QHeaderView : : Fixed ) ;
view - > getTableView ( ) - > horizontalHeader ( ) - > setSectionResizeMode (
0 , QHeaderView : : Stretch ) ;
2018-06-28 19:38:57 +02:00
2018-07-03 14:59:18 +02:00
view - > addButtonPressed . connect ( [ ] {
2018-08-06 21:17:03 +02:00
getApp ( ) - > moderationActions - > items . appendItem (
ModerationAction ( " /timeout {user} 300 " ) ) ;
2018-07-03 14:59:18 +02:00
} ) ;
2018-05-26 20:25:00 +02:00
/*auto taggedUsers = tabs.appendTab(new QVBoxLayout, "Tagged users");
{
2018-06-26 17:06:17 +02:00
EditableModelView * view = * taggedUsers . emplace < EditableModelView > (
2018-05-26 20:25:00 +02:00
app - > taggedUsers - > createModel ( nullptr ) ) ;
view - > setTitles ( { " Name " } ) ;
view - > getTableView ( ) - > horizontalHeader ( ) - > setStretchLastSection ( true ) ;
view - > addButtonPressed . connect ( [ ] {
getApp ( ) - > taggedUsers - > users . appendItem (
2018-06-26 17:06:17 +02:00
TaggedUser ( ProviderId : : Twitch , " example " , " xD " ) ) ;
2018-05-26 20:25:00 +02:00
} ) ;
} */
2018-01-13 03:06:10 +01:00
}
// ---- misc
2018-07-06 19:23:47 +02:00
this - > itemsChangedTimer_ . setSingleShot ( true ) ;
2018-01-12 23:09:05 +01:00
}
2018-01-13 03:06:10 +01:00
2018-01-12 23:09:05 +01:00
} // namespace chatterino