Ejemplo n.º 1
0
/*!
 * \param wid Window ID
 *
 * Runs when the specified Window ID is closed.
 */
void IdealIRC::subWinClosed(int wid)
{
    subwindow_t empty;
    empty.wid = -1;

    subwindow_t sw = winlist.value(wid, empty);

    if (sw.wid == -1)
        return; // Nope.

    if (sw.type == WT_STATUS) {
        int statusCount = 0;

        QHashIterator<int,subwindow_t> i(winlist);
        while (i.hasNext())
            if (i.next().value().type == WT_STATUS)
                ++statusCount;

        if (statusCount <= 1)
            return; // Nope.
    }

    std::cout << "Closing " << sw.widget->objectName().toStdString().c_str() << " (" << wid << ")" << std::endl;
    scriptParent.resetMenuPtrList();

    if (sw.type == WT_STATUS) {
        // Closing a status window.
        QList<QMdiSubWindow*> closeList; // List of subwindows for this status to close
        QHashIterator<int,subwindow_t> i(winlist); // Iterate all windows
        IConnection *con = conlist.value(wid); // Remember window id of status is always equal to connection id. KISS!
        while (i.hasNext()) {
            i.next();
            subwindow_t s = i.value();
            // This item may not be a subwindow of the status that's closing.

            if (s.parent == wid) {
                // This item is a child of the status
                closeList.push_back(s.subwin);
                con->freeWindow( s.widget->objectName() );
            }
        }

        if (con->isSocketOpen())
            con->closeConnection(); // Begin closing the socket.

        // Close all its subwindows
        int count = closeList.count()-1;
        std::cout << "  count : " << count << std::endl;
        for (int i = 0; i <= count; i++) {
            QMdiSubWindow *w = closeList.at(i);
            w->close();
        }

        con->freeWindow("STATUS"); // Free status window lastly
        if (! con->isSocketOpen())
            delete con; // Socket wasn't open, delete connection object

    }

    else {
        IConnection *con = conlist.value(sw.parent, NULL);
        if (con != NULL)
            con->freeWindow( sw.widget->objectName() );
        winlist.remove(sw.wid);
    }

    if (sw.type == WT_CHANNEL) {
        IConnection *con = conlist.value(sw.parent);
        con->sockwrite("PART :" + sw.widget->objectName() );
    }

    if (sw.type == WT_STATUS)
        wsw.delGroup(sw.wid);
    else
        wsw.delWindow(sw.wid);

    ui->treeWidget->removeItemWidget(sw.treeitem, 0);
    delete sw.treeitem;
    delete sw.widget;
    winlist.remove(wid);

    /**
       @note Do we need to delete the other pointers from subwindow_t ?
    **/
}
Ejemplo n.º 2
0
void IdealIRC::closeEvent(QCloseEvent *e)
{
    if (connectionsRemaining > 0) {
        e->ignore(); // Still waiting for connections to close...
        return;
    }

    if (connectionsRemaining == -1) {
        // See if there is any connections active, if so, confirm on exit.
        QHashIterator<int,IConnection*> i(conlist);
        bool hasActive = false;
        while (i.hasNext()) {
            IConnection *c = i.next().value();
            if (c != NULL)
                if (c->isOnline())
                    hasActive = true;
            if (hasActive)
                break;
        }

        if (hasActive) {
            int b = QMessageBox::question(this, tr("Confirm exit"),
                                          tr("There's connections active.\r\nDo you want to exit IdealIRC?"),
                                          QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
            if (b == QMessageBox::No) {
                e->ignore();
                return;
            }
        }
    }

    // Save config prior to exit.
    conf.showToolBar = ui->toolBar->isVisible();
    conf.showTreeView = (ui->treeWidget->width() > 0);
    conf.showButtonbar = wsw.getToolbar()->isVisible();
    conf.maximized = isMaximized();
    conf.save();

    if (connectionsRemaining == 0) {
        e->accept(); // All connections closed, exit.
        return;
    }

    // Iterate through the connections and find the active ones and close them.
    // Remove and clean up their children too (windows and status)
    QHashIterator<int,IConnection*> ic(conlist);
    QList<IConnection*> cl;
    while (ic.hasNext()) {
        ic.next();
        IConnection *con = ic.value();
        if (con->isSocketOpen()) {
            if (connectionsRemaining == -1)
                connectionsRemaining = 0;
            ++connectionsRemaining;
            cl.push_back(con);
        }
    }

    readyToClose = false;
    for (int i = 0; i <= cl.count()-1; i++)
        cl[i]->closeConnection(true);
    readyToClose = true;
    if (! close()) // final close attempt
        e->ignore();
    else
        e->accept();
}