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: GuiRefusedChat.cpp 1455 2020-12-23 10:17:53Z mastroddi $
21 //
22 //////////////////////////////////////////////////////////////////////
23 
24 #include "BeeUtils.h"
25 #include "ChatManager.h"
26 #include "GuiRefusedChat.h"
27 #include "IconManager.h"
28 #include "Settings.h"
29 
30 
GuiRefusedChat(QWidget * parent)31 GuiRefusedChat::GuiRefusedChat( QWidget *parent )
32   : QDialog( parent ), m_refusedChats()
33 {
34   setupUi( this );
35   setWindowTitle( tr( "Blocked chats" ) + QString( " - %1" ).arg( Settings::instance().programName() ) );
36   setWindowIcon( IconManager::instance().icon( "refused-chat.png" ) );
37   Bee::removeContextHelpButton( this );
38 
39   mp_twRefusedChats->setColumnCount( 2 );
40   QStringList labels;
41   labels << tr( "Name" ) << tr( "Blocked chat ID" );
42   mp_twRefusedChats->setHeaderLabels( labels );
43   mp_twRefusedChats->setAlternatingRowColors( true );
44   mp_twRefusedChats->setSortingEnabled( true );
45   mp_twRefusedChats->setRootIsDecorated( false );
46   mp_twRefusedChats->setContextMenuPolicy( Qt::CustomContextMenu );
47   mp_twRefusedChats->setSelectionMode( QAbstractItemView::MultiSelection );
48 
49   QHeaderView* hv = mp_twRefusedChats->header();
50 #if QT_VERSION >= 0x050000
51   hv->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
52   hv->setSectionResizeMode( 1, QHeaderView::Stretch );
53 #else
54   hv->setResizeMode( 0, QHeaderView::ResizeToContents );
55   hv->setResizeMode( 1, QHeaderView::Stretch );
56 #endif
57 
58   mp_menuContext = new QMenu( this );
59   mp_menuContext->addAction( IconManager::instance().icon( "delete.png" ), tr( "Remove blocked chat" ), this, SLOT( removeRefusedChat() ) );
60   mp_menuContext->addSeparator();
61   mp_menuContext->addAction( IconManager::instance().icon( "clear.png" ), tr( "Clear all" ), this, SLOT( removeAllRefusedChats() ) );
62 
63   connect( mp_pbOk, SIGNAL( clicked() ), this, SLOT( saveAndClose() ) );
64   connect( mp_pbCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
65   connect( mp_twRefusedChats, SIGNAL( customContextMenuRequested( const QPoint& ) ), this, SLOT( openCustomMenu( const QPoint& ) ) );
66 }
67 
loadRefusedChatsInList()68 void GuiRefusedChat::loadRefusedChatsInList()
69 {
70   if( mp_twRefusedChats->topLevelItemCount() > 0 )
71     mp_twRefusedChats->clear();
72   foreach( ChatRecord cr, m_refusedChats )
73   {
74     QTreeWidgetItem* item = new QTreeWidgetItem( mp_twRefusedChats );
75     item->setText( 0, cr.name() );
76     item->setIcon( 0, IconManager::instance().icon( "refused-chat.png" ));
77     item->setText( 1, cr.privateId() );
78   }
79 }
80 
loadRefusedChats()81 int GuiRefusedChat::loadRefusedChats()
82 {
83   if( !m_refusedChats.isEmpty() )
84     m_refusedChats.clear();
85 
86   m_refusedChats = ChatManager::instance().refusedChats();
87   loadRefusedChatsInList();
88   return m_refusedChats.size();
89 }
90 
saveAndClose()91 void GuiRefusedChat::saveAndClose()
92 {
93   ChatManager::instance().clearRefusedChats();
94   foreach( ChatRecord cr, m_refusedChats )
95     ChatManager::instance().addToRefusedChat( cr );
96   accept();
97 }
98 
openCustomMenu(const QPoint & p)99 void GuiRefusedChat::openCustomMenu( const QPoint& p )
100 {
101   QTreeWidgetItem* item = mp_twRefusedChats->itemAt( p );
102   if( !item )
103     return;
104 
105   if( !item->isSelected() )
106     item->setSelected( true );
107 
108   mp_menuContext->exec( QCursor::pos() );
109 }
110 
removeAllRefusedChats()111 void GuiRefusedChat::removeAllRefusedChats()
112 {
113   m_refusedChats.clear();
114   loadRefusedChatsInList();
115 }
116 
removeRefusedChat()117 void GuiRefusedChat::removeRefusedChat()
118 {
119   QList<QTreeWidgetItem*> items = mp_twRefusedChats->selectedItems();
120   if( items.isEmpty() )
121   {
122     QMessageBox::information( this, Settings::instance().programName(), tr( "Please select an item in the list." ) );
123     return;
124   }
125 
126   foreach( QTreeWidgetItem* item, items )
127   {
128     ChatRecord cr( item->text( 0 ), item->text( 1 ) );
129     m_refusedChats.removeOne( cr );
130   }
131 
132   loadRefusedChatsInList();
133 }
134