1 //////////////////////////////////////////////////////////////////////
2 //
3 // BeeBEEP Copyright (C) 2010-2021 Marco Mastroddi
4 //
5 // BeeBEEP is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published
7 // by the Free Software Foundation, either version 3 of the License,
8 // or (at your option) any later version.
9 //
10 // BeeBEEP is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with BeeBEEP. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Author: Marco Mastroddi <marco.mastroddi(AT)gmail.com>
19 //
20 // $Id: GuiHome.cpp 1468 2021-01-05 13:15:58Z mastroddi $
21 //
22 //////////////////////////////////////////////////////////////////////
23 
24 #include "BeeUtils.h"
25 #include "GuiHome.h"
26 #include "GuiChatMessage.h"
27 #include "ChatManager.h"
28 #include "ChatMessage.h"
29 #include "IconManager.h"
30 #include "Settings.h"
31 #include "ShortcutManager.h"
32 
33 
GuiHome(QWidget * parent)34 GuiHome::GuiHome( QWidget* parent )
35   : QWidget( parent )
36 {
37   setObjectName( "GuiHome" );
38   setupUi( this );
39   m_prev_sys_mess = "";
40 
41   mp_teSystem->setObjectName( "GuiSystemViewer" );
42   mp_teSystem->setFocusPolicy( Qt::ClickFocus );
43   mp_teSystem->setReadOnly( true );
44   mp_teSystem->setContextMenuPolicy( Qt::CustomContextMenu );
45   mp_teSystem->setOpenExternalLinks( false );
46   mp_teSystem->setOpenLinks( false );
47 
48   mp_lNews->setOpenExternalLinks( true );
49 
50   mp_menuContext = new QMenu( this );
51 
52   connect( mp_teSystem, SIGNAL( customContextMenuRequested( const QPoint& ) ), this, SLOT( customContextMenu( const QPoint& ) ) );
53   connect( mp_teSystem, SIGNAL( anchorClicked( const QUrl& ) ), this, SLOT( checkAnchorClicked( const QUrl&  ) ) );
54 }
55 
addSystemMessage(const ChatMessage & cm)56 bool GuiHome::addSystemMessage( const ChatMessage& cm )
57 {
58   if( !cm.isSystemActivity() )
59     return false;
60 
61   QString sys_message = GuiChatMessage::formatSystemMessage( cm, ID_SYSTEM_MESSAGE, Settings::instance().homeShowMessageTimestamp(), false, Settings::instance().chatCompact() );
62 
63   if( sys_message.isEmpty() )
64     return false;
65 
66   if( sys_message == m_prev_sys_mess )
67     return false;
68 
69   m_prev_sys_mess = sys_message;
70 
71   QTextCursor cursor( mp_teSystem->textCursor() );
72   cursor.movePosition( QTextCursor::End );
73   cursor.insertHtml( sys_message );
74   QScrollBar *bar = mp_teSystem->verticalScrollBar();
75   if( bar )
76     bar->setValue( bar->maximum() );
77   return true;
78 }
79 
checkAnchorClicked(const QUrl & url)80 void GuiHome::checkAnchorClicked( const QUrl& url )
81 {
82   emit openUrlRequest( url );
83 }
84 
customContextMenu(const QPoint &)85 void GuiHome::customContextMenu( const QPoint& )
86 {
87   mp_menuContext->clear();
88   mp_menuContext->addAction( IconManager::instance().icon( "background-color.png" ), tr( "Change background color" ) + QString("..."), this, SLOT( selectBackgroundColor() ) );
89   mp_menuContext->addSeparator();
90   mp_menuContext->addAction( IconManager::instance().icon( "select-all.png" ), tr( "Select all" ), mp_teSystem, SLOT( selectAll() ), QKeySequence::SelectAll );
91   mp_menuContext->addSeparator();
92   QAction* act = mp_menuContext->addAction( IconManager::instance().icon( "copy.png" ), tr( "Copy to clipboard" ), mp_teSystem, SLOT( copy() ), QKeySequence::Copy );
93   act->setEnabled( !mp_teSystem->textCursor().selectedText().isEmpty() );
94   mp_menuContext->addSeparator();
95   act = mp_menuContext->addAction( IconManager::instance().icon( "printer.png" ), tr( "Print..." ), this, SLOT( printActivities() ) );
96   QKeySequence ks = ShortcutManager::instance().shortcut( ShortcutManager::Print );
97   if( !ks.isEmpty() && Settings::instance().useShortcuts() )
98     act->setShortcut( ks );
99   else
100     act->setShortcut( QKeySequence() );
101   mp_menuContext->addSeparator();
102   act = mp_menuContext->addAction( tr( "Show the timestamp" ), this, SLOT( onAddTimestampClicked() ) );
103   act->setCheckable( true );
104   act->setChecked( Settings::instance().homeShowMessageTimestamp() );
105   mp_menuContext->addSeparator();
106   act = mp_menuContext->addAction( IconManager::instance().icon( "clear.png" ), tr( "Clear system messages" ), this, SLOT( clearSystemMessages() ) );
107   mp_menuContext->exec( QCursor::pos() );
108 }
109 
loadSystemMessages()110 int GuiHome::loadSystemMessages()
111 {
112   int num_sys_msg = 0;
113   mp_teSystem->clear();
114   Chat c = ChatManager::instance().defaultChat();
115   foreach( ChatMessage cm, c.messages() )
116   {
117     if( addSystemMessage( cm ) )
118       num_sys_msg++;
119   }
120   QTimer::singleShot( 0, this, SLOT( resetNews() ) );
121   return num_sys_msg;
122 }
123 
clearSystemMessages()124 void GuiHome::clearSystemMessages()
125 {
126   emit clearSystemMessagesRequest( ID_DEFAULT_CHAT );
127 }
128 
resetNews()129 void GuiHome::resetNews()
130 {
131   setNews( "" );
132 }
133 
setNews(const QString & news)134 void GuiHome::setNews( const QString& news )
135 {
136   QString tooltip_text = "";
137   if( news.isEmpty() )
138   {
139     mp_lNews->setText( QString( "<a style='text-decoration: none;' href='%1'><b>%2</b></a>" )
140                          .arg( Settings::instance().newsWebSite() )
141                          .arg( Bee::beeColorsToHtmlText( "B  e  e  B  E  E  P" ) ) );
142     tooltip_text = tr( "Click here to see the latest news about BeeBEEP project" );
143   }
144   else
145   {
146     mp_lNews->setText( news );
147     tooltip_text = tr( "Click here to read more" );
148   }
149 
150   if( !tooltip_text.isEmpty() )
151     mp_lNews->setToolTip( QString( "<span style='color: %1'>%2</span>" ).arg( Bee::colorYellow().name() ).arg( tooltip_text ) );
152 }
153 
reloadMessages()154 void GuiHome::reloadMessages()
155 {
156   QApplication::setOverrideCursor( Qt::WaitCursor );
157   mp_teSystem->clear();
158   loadSystemMessages();
159   QApplication::restoreOverrideCursor();
160 }
161 
onAddTimestampClicked()162 void GuiHome::onAddTimestampClicked()
163 {
164   QAction* act = qobject_cast<QAction*>( sender() );
165   if( !act )
166     return;
167 
168   Settings::instance().setHomeShowMessageTimestamp( act->isChecked() );
169   QTimer::singleShot( 100, this, SLOT( reloadMessages() ) );
170 }
171 
printActivities()172 void GuiHome::printActivities()
173 {
174   QPrinter printer( QPrinter::HighResolution );
175   printer.setFullPage( true );
176   QPrintDialog *dlg = new QPrintDialog( &printer, this );
177   dlg->setOptions( QAbstractPrintDialog::PrintSelection | QAbstractPrintDialog::PrintPageRange |
178                    QAbstractPrintDialog::PrintShowPageSize |  QAbstractPrintDialog::PrintCollateCopies |
179 #if QT_VERSION >= 0x040700
180                    QAbstractPrintDialog::PrintCurrentPage |
181 #endif
182                    QAbstractPrintDialog::PrintToFile );
183 
184   if( dlg->exec() == QDialog::Accepted)
185     mp_teSystem->print( dlg->printer() );
186 
187   dlg->deleteLater();
188 }
189 
selectBackgroundColor()190 void GuiHome::selectBackgroundColor()
191 {
192   QColor c = Bee::selectColor( this, Settings::instance().homeBackgroundColor() );
193   if( c.isValid() )
194   {
195     Settings::instance().setHomeBackgroundColor( c.name() );
196     updateBackground();
197   }
198 }
199 
updateBackground()200 void GuiHome::updateBackground()
201 {
202   mp_teSystem->setStyleSheet( QString( "#GuiSystemViewer { background-color: %1; }" ).arg( Settings::instance().homeBackgroundColor() ) );
203 }
204