~mateo-salta/nitroshare/nitroshare

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* NitroShare - A simple network file sharing tool.
   Copyright (C) 2012 Nathan Osman

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <QCoreApplication>
#include <QFileDialog>
#include <QIcon>
#include <QMessageBox>
#include <QStringList>

#include <dialogs/CAboutDialog.h>
#include <dialogs/CMachineSelectionDialog.h>
#include <dialogs/CSettingsDialog.h>
#include <file/CFileSender.h>
#include <main/CTrayIcon.h>
#include <util/settings.h>

CTrayIcon::CTrayIcon()
    : m_pc_menu("Send Files To")
{
    // Connect the signals for the broadcast and file servers
    connect(&m_broadcast_server, SIGNAL(Error(QString)),       SLOT(OnError(QString)));
    connect(&m_broadcast_server, SIGNAL(Information(QString)), SLOT(OnInformation(QString)));

    connect(&m_file_server, SIGNAL(Message(QString,QSystemTrayIcon::MessageIcon)),
                            SLOT(OnInformation(QString)));

    // Initialize the two servers
    m_broadcast_server.Init();
    m_file_server.Init();

    connect(&m_pc_menu, SIGNAL(aboutToShow()), SLOT(OnShowMachineList()));

    // Initialize the icon
    setIcon(QIcon(":/icons/light.png"));

    /* Create the menu and the share boxes. */
    CreateContextMenu();
    CreateShareBoxes();

    // Display the icon and the sharebox
    setVisible(true);
}

CTrayIcon::~CTrayIcon()
{
    /* Free all of the ShareBoxes. */
    qDeleteAll(m_shareboxes);
}

void CTrayIcon::OnAbout()
{
    CAboutDialog().exec();
}

void CTrayIcon::OnAddShareBox()
{
    CMachineSelectionDialog dialog(m_broadcast_server.GetMachineMap(),
                                   tr("Please select a machine to create a ShareBox for:"), true);

    if(dialog.exec())
    {
        QByteArray name;

        MachineMap machines = m_broadcast_server.GetMachineMap();
        if(machines.contains(dialog.GetMachineID()))
            name = machines[dialog.GetMachineID()].name;

        CreateShareBox(dialog.GetMachineID(), name);
        UpdateShareBoxes();
    }
}

void CTrayIcon::OnError(QString message)
{
    if(Settings::Notify("Error"))
        showMessage("NitroShare Error", message, QSystemTrayIcon::Critical);
}

void CTrayIcon::OnInformation(QString message)
{
    showMessage("NitroShare Notification", message, QSystemTrayIcon::Information);
}

void CTrayIcon::OnRemoveShareBox()
{
    CShareBox * box = qobject_cast<CShareBox *>(sender());
    m_shareboxes.removeOne(box);
    delete box;

    UpdateShareBoxes();
}

void CTrayIcon::OnSendFiles()
{
    // Ask the user for the files they wish to send
    QStringList filenames = QFileDialog::getOpenFileNames(NULL, "Select Files to Send");
    if(filenames.size())
    {
        QAction * action = qobject_cast<QAction *>(sender());
        OnSendFiles(filenames, action->data().toString());
    }
}

void CTrayIcon::OnSendFiles(QStringList filenames, QString id)
{
    if(id.isEmpty())
    {
        /* There's no point showing the dialog to the user if there are no
           machines currently available to send to. */
        if(m_broadcast_server.AnyMachinesOnline())
        {
            CMachineSelectionDialog dialog(m_broadcast_server.GetMachineMap(),
                                           "Please select a machine to send these files to:");

            if(dialog.exec()) id = dialog.GetMachineID();
            else              return;
        }
        else
        {
            QMessageBox::critical(NULL, "Error:", "There are currently no machines on the local network that can receive files.");
            return;
        }
    }
    /* If this method was invoked with a specific ID, then ensure
       that the machine does indeed exist on the network currently. */
    else if(!m_broadcast_server.MachineExists(id))
    {
        QMessageBox::critical(NULL, "Error:", "This machine is currently offline.");
        return;
    }

    /* Create the client that will manage the transmission of files. */
    CFileSender * client = new CFileSender(this, filenames);
    connect(client, SIGNAL(Error(QString)),       SLOT(OnError(QString)));
    connect(client, SIGNAL(Information(QString)), SLOT(OnInformation(QString)));

    /* If the object emitting the signal is a CShareBox, then we can use
    it to display progress of the transfer. */
    CShareBox * box = qobject_cast<CShareBox *>(sender());
    if(box)
        connect(client, SIGNAL(Progress(int)), box, SLOT(OnProgress(int)));

    client->Start(m_broadcast_server.GetMachine(id));
}

void CTrayIcon::OnSettings()
{
    /* Display the settings dialog. */
    if(CSettingsDialog().exec())
    {
        /* If the user accepted the dialog, then re-initialize
           the broadcast and file servers. */
        m_broadcast_server.Init();
        m_file_server.Init();
    }
}

void CTrayIcon::OnShowMachineList()
{
    /* Clear the list and add each of the machines to it. */
    m_pc_menu.clear();

    for(MachineMap::const_iterator i = m_broadcast_server.GetMachineMap().constBegin();
        i != m_broadcast_server.GetMachineMap().constEnd(); ++i)
    {
        QAction * action = m_pc_menu.addAction(i.value().name, this, SLOT(OnSendFiles()));
        action->setData(i.key());
    }
}

void CTrayIcon::UpdateShareBoxes()
{
    /* Loop through each of the share boxes, storing their information. */
    QVariantList boxes;

    foreach(CShareBox * box, m_shareboxes)
    {
        QVariantMap map;
        map["id"]   = box->GetMachineID();
        map["name"] = box->GetMachineName();
        map["pos"]  = box->pos();

        boxes << QVariant(map);
    }

    /* Store the list. */
    Settings::Set("ShareBox/Instances", QVariant(boxes));
}

void CTrayIcon::CreateContextMenu()
{
    QMenu * menu = new QMenu;

    menu->addMenu(&m_pc_menu);
    menu->addSeparator();
    menu->addAction(tr("Add ShareBox"), this, SLOT(OnAddShareBox()));
    menu->addSeparator();
    menu->addAction(tr("Settings"), this, SLOT(OnSettings()));
    menu->addAction(tr("About"),    this, SLOT(OnAbout()));
    menu->addSeparator();
    menu->addAction(tr("Exit"), QCoreApplication::instance(), SLOT(quit()));

    setContextMenu(menu);
}

CShareBox * CTrayIcon::CreateShareBox(QString id, QByteArray name)
{
    CShareBox * box = new CShareBox();
    box->SetMachineInfo(id, name);

    connect(box, SIGNAL(AddShareBox()), SLOT(OnAddShareBox()));
    connect(box, SIGNAL(FilesDropped(QStringList,QString)),
                 SLOT(OnSendFiles(QStringList,QString)));
    connect(box, SIGNAL(Moved()), SLOT(UpdateShareBoxes()));
    connect(box, SIGNAL(Remove()), SLOT(OnRemoveShareBox()));

    m_shareboxes.append(box);
    return box;
}

void CTrayIcon::CreateShareBoxes()
{
    /* Load the list of ShareBoxes from the settings file. Each
       one contains a serialized set of properties indicating what
       it is for and where it goes onscreen, etc. */
    QVariantList boxes = Settings::Get("ShareBox/Instances").toList();

    foreach(QVariant variant, boxes)
    {
        QVariantMap map = variant.toMap();

        /* Each entry contains the position of the box and the unique ID
           of the machine it is for. */
        CreateShareBox(map["id"].toString(), map["name"].toByteArray())->move(map["pos"].toPoint());
    }
}