void MainWindow::connectOpened() { // Sending broadcast hello mProtocol->initSockets(); connect(mProtocol, SIGNAL(peerListChanged()), this, SLOT(refreshPeerList())); connect(mProtocol, SIGNAL(sendFileComplete(QStringList*)), this, SLOT(sendFileComplete(QStringList*))); connect(mProtocol, SIGNAL(sendFileError(int)), this, SLOT(sendFileError(int))); connect(mProtocol, SIGNAL(receiveFileStart()), this, SLOT(receiveFileStart())); connect(mProtocol, SIGNAL(receiveFileComplete(QStringList*)), this, SLOT(receiveFileComplete(QStringList*))); connect(mProtocol, SIGNAL(receiveFileCancelled()), this, SLOT(receiveFileCancelled())); connect(mProtocol, SIGNAL(transferStatusUpdate(int)), this, SLOT(transferStatusUpdate(int)), Qt::DirectConnection); connect(mProtocol, SIGNAL(receiveTextComplete(QString*)), this, SLOT(receiveTextComplete(QString*))); mProtocol->sayHello(QHostAddress::Broadcast); // Hide connecting dialog mConnectingDialog->close(); delete mConnectingDialog; mConnectingDialog = NULL; }
void MainWindow::connectOpened() { // Sending broadcast hello mProtocol->initSockets(); connect(mProtocol, SIGNAL(peerListChanged()), this, SLOT(refreshPeerList())); connect(mProtocol, SIGNAL(sendFileComplete(QStringList*)), this, SLOT(sendFileComplete(QStringList*))); connect(mProtocol, SIGNAL(sendFileError(int)), this, SLOT(sendFileError(int))); connect(mProtocol, SIGNAL(receiveFileStart()), this, SLOT(receiveFileStart())); connect(mProtocol, SIGNAL(receiveFileComplete(QStringList*)), this, SLOT(receiveFileComplete(QStringList*))); connect(mProtocol, SIGNAL(receiveFileCancelled()), this, SLOT(receiveFileCancelled())); connect(mProtocol, SIGNAL(transferStatusUpdate(int)), this, SLOT(transferStatusUpdate(int)), Qt::DirectConnection); connect(mProtocol, SIGNAL(receiveTextComplete(QString*)), this, SLOT(receiveTextComplete(QString*))); // mProtocol->sayHello(QHostAddress::Broadcast); // Hide connecting dialog mConnectingDialog->close(); delete mConnectingDialog; mConnectingDialog = NULL; // Timer per l'invio dell'hello (a volte non viene inviato se lo invio subito) QTimer::singleShot(200, this, SLOT(sayHelloAgain())); }
// Chiusura della connessione TCP in ricezione void DuktoProtocol::closedConnection() { // Svuoto il buffer in ricezione readNewData(); // Chiusura eventuale file corrente if (mCurrentFile) { QString name; name = mCurrentFile->fileName(); mCurrentFile->close(); delete mCurrentFile; mCurrentFile = NULL; QFile::remove(name); receiveFileCancelled(); } // Ricezione file conclusa else if (!mReceivingText) receiveFileComplete(mReceivedFiles); // Ricezione testo conclusa else { QString rec = QString::fromUtf8(mTextToReceive); receiveTextComplete(&rec); } // Chiusura socket if (mCurrentSocket) { mCurrentSocket->disconnect(); mCurrentSocket->disconnectFromHost(); mCurrentSocket->close(); mCurrentSocket->deleteLater(); mCurrentSocket = NULL; } // Rilascio memoria delete mReceivedFiles; mReceivedFiles = NULL; // Impostazione stato mIsReceiving = false; }
// Chiusura della connessione TCP in ricezione void DuktoProtocol::closedConnection() { // empty the receive buffer readNewData(); // Closing any current file if (mCurrentFile) { QString name; name = mCurrentFile->fileName(); mCurrentFile->close(); delete mCurrentFile; mCurrentFile = NULL; QFile::remove(name); receiveFileCancelled(); } else if (!mReceivingText) // Receiving file ended { receiveFileComplete(mReceivedFiles, mTotalSize); // TODO: notify for recieving file } else // Receiving text ended { QString rec = QString::fromUtf8(mTextToReceive); receiveTextComplete(&rec, mTotalSize); // TODO: notify for recieving text } // closing socket if (mCurrentSocket) { mCurrentSocket->disconnect(); mCurrentSocket->disconnectFromHost(); mCurrentSocket->close(); mCurrentSocket->deleteLater(); mCurrentSocket = NULL; } // release memory delete mReceivedFiles; mReceivedFiles = NULL; // reset status mIsReceiving = false; }
// Processo di lettura principale void DuktoProtocol::readNewData() { // Fino a che ci sono dati da leggere while (mCurrentSocket->bytesAvailable() > 0) { // In base allo stato in cui mi trovo leggo quello che mi aspetto switch (mRecvStatus) { case FILENAME: { char c; while (true) { if (mCurrentSocket->read(&c, sizeof(c)) < 1) return; if (c == '\0') { mRecvStatus = FILESIZE; break; } mPartialName.append(c); } break; } case FILESIZE: { if(mCurrentSocket->bytesAvailable() < (int)sizeof(qint64)) return; mCurrentSocket->read((char*) &mElementSize, sizeof(qint64)); mElementReceivedData = 0; QString name = QString::fromUtf8(mPartialName); mPartialName.clear(); // Se l'elemento corrente è una cartella, la creo e passo all'elemento successivo if (mElementSize == -1) { // Verifico il nome della cartella "root" // Se non ho ancora trattato questa root, lo faccio ora if (mRootFolderName != name.section("/", 0, 0)) { // Verifico se ho già una cartella con questo nome // nel caso trovo un nome alternativo int i = 2; QString originalName = name; while (QFile::exists(name)) name = originalName + " (" + QString::number(i++) + ")"; mRootFolderName = originalName; mRootFolderRenamed = name; mReceivedFiles->append(name); } // Se invece l'ho già trattata, allora rinomino questo percorso else if (mRootFolderName != mRootFolderRenamed) name = name.replace(0, name.indexOf('/'), mRootFolderRenamed); // Creo la cartella if (!QDir(".").mkpath(name)) { emit receiveFileCancelled(); // Chiusura socket if (mCurrentSocket) { mCurrentSocket->disconnect(); mCurrentSocket->disconnectFromHost(); mCurrentSocket->close(); mCurrentSocket->deleteLater(); mCurrentSocket = NULL; } // Rilascio memoria delete mReceivedFiles; mReceivedFiles = NULL; // Impostazione stato mIsReceiving = false; return; } mRecvStatus = FILENAME; break; } // Potrebbe essere un invio di testo else if (name == "___DUKTO___TEXT___") { mReceivedFiles->append(name); mReceivingText = true; mTextToReceive.clear(); mCurrentFile = NULL; } // Altrimenti creo il nuovo file else { // Se il file è in una cartella rinominata, devo provvedere di conseguenza if ((name.indexOf('/') != -1) && (name.section("/", 0, 0) == mRootFolderName)) name = name.replace(0, name.indexOf('/'), mRootFolderRenamed); // Se il file esiste già cambio il nome di quello nuovo int i = 2; QString originalName = name; while (QFile::exists(name)) { QFileInfo fi(originalName); name = fi.baseName() + " (" + QString::number(i) + ")." + fi.completeSuffix(); i++; } mReceivedFiles->append(name); mCurrentFile = new QFile(name); bool ret = mCurrentFile->open(QIODevice::WriteOnly); if (!ret) { emit receiveFileCancelled(); // Chiusura socket if (mCurrentSocket) { mCurrentSocket->disconnect(); mCurrentSocket->disconnectFromHost(); mCurrentSocket->close(); mCurrentSocket->deleteLater(); mCurrentSocket = NULL; } // Rilascio memoria delete mReceivedFiles; mReceivedFiles = NULL; // Impostazione stato mIsReceiving = false; return; } mReceivingText = false; } mRecvStatus = DATA; break; } case DATA: { // Provo a leggere quanto mi serve per finire il file corrente // (o per svuotare il buffer dei dati ricevuti) qint64 s = (mCurrentSocket->bytesAvailable() > (mElementSize - mElementReceivedData)) ? (mElementSize - mElementReceivedData) : mCurrentSocket->bytesAvailable(); QByteArray d = mCurrentSocket->read(s); mElementReceivedData += d.size(); mTotalReceivedData += d.size(); updateStatus(); // Salvo i dati letti if (!mReceivingText) mCurrentFile->write(d); else mTextToReceive.append(d); // Verifico se ho completato l'elemento corrente if (mElementReceivedData == mElementSize) { // Completato, chiudo il file e mi preparo per il prossimo elemento mElementSize = -1; if (!mReceivingText) { mCurrentFile->deleteLater(); mCurrentFile = NULL; } mRecvStatus = FILENAME; } break; } } } }
GuiBehind::GuiBehind(DuktoWindow* view) : QObject(NULL), mView(view), mShowBackTimer(NULL), mPeriodicHelloTimer(NULL), mClipboard(NULL), mMiniWebServer(NULL), mSettings(NULL), mDestBuddy(NULL), mUpdatesChecker(NULL) { // Status variables mView->setGuiBehindReference(this); setCurrentTransferProgress(0); setTextSnippetSending(false); setShowUpdateBanner(false); // Clipboard object mClipboard = QApplication::clipboard(); connect(mClipboard, SIGNAL(dataChanged()), this, SLOT(clipboardChanged())); clipboardChanged(); // Add "Me" entry mBuddiesList.addMeElement(); // Add "Ip" entry mBuddiesList.addIpElement(); // Settings mSettings = new Settings(this); // Mini web server mMiniWebServer = new MiniWebServer(NETWORK_PORT + 1); // Destination buddy mDestBuddy = new DestinationBuddy(this); // Change current folder QDir::setCurrent(mSettings->currentPath()); // Set current theme color mTheme.setThemeColor(mSettings->themeColor()); // Init buddy list view->rootContext()->setContextProperty("buddiesListData", &mBuddiesList); view->rootContext()->setContextProperty("recentListData", &mRecentList); view->rootContext()->setContextProperty("ipAddressesData", &mIpAddresses); view->rootContext()->setContextProperty("guiBehind", this); view->rootContext()->setContextProperty("destinationBuddy", mDestBuddy); view->rootContext()->setContextProperty("theme", &mTheme); // Register protocol signals connect(&mDuktoProtocol, SIGNAL(peerListAdded(Peer)), this, SLOT(peerListAdded(Peer))); connect(&mDuktoProtocol, SIGNAL(peerListRemoved(Peer)), this, SLOT(peerListRemoved(Peer))); connect(&mDuktoProtocol, SIGNAL(receiveFileStart(QString)), this, SLOT(receiveFileStart(QString))); connect(&mDuktoProtocol, SIGNAL(transferStatusUpdate(qint64,qint64)), this, SLOT(transferStatusUpdate(qint64,qint64))); connect(&mDuktoProtocol, SIGNAL(receiveFileComplete(QStringList*,qint64)), this, SLOT(receiveFileComplete(QStringList*,qint64))); connect(&mDuktoProtocol, SIGNAL(receiveTextComplete(QString*,qint64)), this, SLOT(receiveTextComplete(QString*,qint64))); connect(&mDuktoProtocol, SIGNAL(sendFileComplete(QStringList*)), this, SLOT(sendFileComplete(QStringList*))); connect(&mDuktoProtocol, SIGNAL(sendFileError(int)), this, SLOT(sendFileError(int))); connect(&mDuktoProtocol, SIGNAL(receiveFileCancelled()), this, SLOT(receiveFileCancelled())); connect(&mDuktoProtocol, SIGNAL(sendFileAborted()), this, SLOT(sendFileAborted())); // Register other signals connect(this, SIGNAL(remoteDestinationAddressChanged()), this, SLOT(remoteDestinationAddressHandler())); // Say "hello" mDuktoProtocol.setPorts(NETWORK_PORT, NETWORK_PORT); mDuktoProtocol.initialize(); mDuktoProtocol.sayHello(QHostAddress::Broadcast); // Periodic "hello" timer mPeriodicHelloTimer = new QTimer(this); connect(mPeriodicHelloTimer, SIGNAL(timeout()), this, SLOT(periodicHello())); mPeriodicHelloTimer->start(60000); // Load GUI view->setSource(QUrl("qrc:/qml/dukto/Dukto.qml")); //view->setSource(QUrl::fromLocalFile("c:/users/emanuele/documenti/dukto/qml/dukto/Dukto.qml")); #ifndef Q_WS_S60 view->restoreGeometry(mSettings->windowGeometry()); #endif // Start random rotate mShowBackTimer = new QTimer(this); connect(mShowBackTimer, SIGNAL(timeout()), this, SLOT(showRandomBack())); qsrand(QDateTime::currentDateTime().toTime_t());; mShowBackTimer->start(10000); // Enqueue check for updates mUpdatesChecker = new UpdatesChecker(); connect(mUpdatesChecker, SIGNAL(updatesAvailable()), this, SLOT(showUpdatesMessage())); QTimer::singleShot(2000, mUpdatesChecker, SLOT(start())); // TEMP // Peer p(QHostAddress("172.16.3.3"), "Pippo at Pluto (Macintosh)"); // Peer p(QHostAddress("172.16.3.3"), "NomeUtenteMoltoLungoCheNonCiSta at IlMioPcCheHaUnNomeImpensabilmenteLungo (Macintosh)", 4644); // peerListAdded(p); }