summaryrefslogtreecommitdiffstats
path: root/pica-client/mainwindow.cpp
blob: a0bfc440523695254b1c7df0deb12b91fd3465b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "mainwindow.h"
#include "globals.h"
#include "skynet.h"
#include "picaactioncenter.h"
#include "accounts.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QVBoxLayout *layout;

    layout = new QVBoxLayout;
    contact_list = new ContactListWidget;
    status = new QComboBox;
    notifications = new QListWidget();
    bt_showhide_notifications = new QPushButton();
    hide_notifications();

    status->setMinimumHeight(36);
    notifications->setMinimumHeight(48);

    layout->addWidget(contact_list, 8);
    layout->addWidget(bt_showhide_notifications, 0, Qt::AlignRight);
    layout->addWidget(notifications, 1, Qt::AlignBottom);
    layout->addWidget(status,0,Qt::AlignBottom);

    centralWidget()->setLayout(layout);
    setWindowIcon(picapica_ico_sit);

    createMenus();


    //for (int i=0;i<255;i++)
    //    contact_list->addItem(QString("test %1").arg(i));

    status->addItem(picapica_ico_fly, QString(tr("Connected")));
    status->addItem(picapica_ico_sit, QString(tr("Disconnected")));

    connect(skynet, SIGNAL(BecameSelfAware()), this, SLOT(set_online()));
    connect(skynet, SIGNAL(LostSelfAwareness()), this, SLOT(set_offline()));
    connect(status, SIGNAL(currentIndexChanged(int)), this, SLOT(status_changed(int)));
    connect(bt_showhide_notifications, SIGNAL(clicked()), this, SLOT(showhide_click()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::set_offline()
{
    SetStatus(false);
}

void MainWindow::set_online()
{
    SetStatus(true);
}

void MainWindow::status_changed(int index)
{
    if (status_change_disable_flag)
        return;

    if (index == 0 && !skynet->isSelfAware())
    {
        Accounts::AccountRecord curr_acc = skynet->CurrentAccount();
        skynet->Join(curr_acc);
    }
    if (1 == index && skynet->isSelfAware())
        skynet->Exit();
}

void MainWindow::SetStatus(bool connected)
{
    status_change_disable_flag = true;

    if (connected)
    {
        status->setCurrentIndex(0);
        setWindowTitle(tr("Pica Pica Messenger - ") + QByteArray((const char*)account_id, PICA_ID_SIZE).toBase64());
    }
    else
    {
        status->setCurrentIndex(1);
        setWindowTitle(tr("Pica Pica Messenger - Disconnected"));
    }

    status_change_disable_flag = false;
}


void MainWindow::createMenus()
{
    contactsMenu=menuBar()->addMenu(tr("&Contacts"));
    contactsMenu->addAction(contact_list->addcontactAct);
    contactsMenu->addSeparator();
    contactsMenu->addAction(action_center->ExitAct());

    nodesMenu=menuBar()->addMenu(tr("&Nodes"));

    helpMenu=menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(action_center->AboutAct());

}

void MainWindow::show_notifications()
{
    notifications->show();
    bt_showhide_notifications->setText(tr("Hide Notifications"));
    status_notifications_hidden = false;
}

void MainWindow::hide_notifications()
{
    notifications->hide();
    bt_showhide_notifications->setText(tr("Show Notifications"));
    status_notifications_hidden = true;
}

void MainWindow::showhide_click()
{
    if (status_notifications_hidden)
        show_notifications();
    else
        hide_notifications();
}

void MainWindow::AddNotification(QString &text)
{
    notifications->addItem(text);
    notifications->item(notifications->count() -1) ->setForeground(QBrush(QColor(230, 10, 10, 196)));
    show_notifications();
}