diff --git a/libretroshare/src/serialiser/rsmsgitems.cc b/libretroshare/src/serialiser/rsmsgitems.cc index 45af3ba0c..5a7957988 100644 --- a/libretroshare/src/serialiser/rsmsgitems.cc +++ b/libretroshare/src/serialiser/rsmsgitems.cc @@ -428,6 +428,11 @@ uint32_t RsMsgSerialiser::sizeItem(RsMsgItem *item) s += item->msgbcc.TlvSize(); s += item->attachment.TlvSize(); + if (m_bConfiguration) { + // serialise msgId too + s += 4; + } + return s; } @@ -467,6 +472,12 @@ bool RsMsgSerialiser::serialiseItem(RsMsgItem *item, void *data, uint32_t *p ok &= item->msgbcc.SetTlv(data, tlvsize, &offset); ok &= item->attachment.SetTlv(data, tlvsize, &offset); + + if (m_bConfiguration) { + // serialise msgId too + ok &= setRawUInt32(data, tlvsize, &offset, item->msgId); + } + if (offset != tlvsize) { ok = false; @@ -521,6 +532,12 @@ RsMsgItem *RsMsgSerialiser::deserialiseItem(void *data, uint32_t *pktsize) ok &= item->msgbcc.GetTlv(data, rssize, &offset); ok &= item->attachment.GetTlv(data, rssize, &offset); + if (m_bConfiguration) { + // deserialise msgId too + // ok &= getRawUInt32(data, rssize, &offset, &(item->msgId)); + getRawUInt32(data, rssize, &offset, &(item->msgId)); //use this line for backward compatibility + } + if (offset != rssize) { /* error */ diff --git a/libretroshare/src/serialiser/rsmsgitems.h b/libretroshare/src/serialiser/rsmsgitems.h index d304a10a1..730f01a83 100644 --- a/libretroshare/src/serialiser/rsmsgitems.h +++ b/libretroshare/src/serialiser/rsmsgitems.h @@ -179,12 +179,12 @@ std::ostream &print(std::ostream &out, uint16_t indent = 0); class RsMsgSerialiser: public RsSerialType { public: - RsMsgSerialiser() - :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG) + RsMsgSerialiser(bool bConfiguration = false) + :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_MSG), m_bConfiguration (bConfiguration) { return; } RsMsgSerialiser(uint16_t type) - :RsSerialType(RS_PKT_VERSION_SERVICE, type) + :RsSerialType(RS_PKT_VERSION_SERVICE, type), m_bConfiguration (false) { return; } virtual ~RsMsgSerialiser() { return; } @@ -200,6 +200,7 @@ virtual uint32_t sizeItem(RsMsgItem *); virtual bool serialiseItem (RsMsgItem *item, void *data, uint32_t *size); virtual RsMsgItem *deserialiseItem(void *data, uint32_t *size); + bool m_bConfiguration; // is set to true for saving configuration (enables serialising msgId) }; /**************************************************************************/ diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index 58916350b..2be839e77 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -276,10 +276,15 @@ bool p3MsgService::saveConfiguration() std::string msgfile = Filename(); std::string msgfiletmp = Filename()+".tmp"; + if (RsDirUtil::createBackup (msgfile) == false) { + getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File backup error", "Error while backing up file " + msgfile); + // no error ? + } + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ RsSerialiser *rss = new RsSerialiser(); - rss->addSerialType(new RsMsgSerialiser()); + rss->addSerialType(new RsMsgSerialiser(true)); // create serialiser for configuration BinFileInterface *out = new BinFileInterface(msgfiletmp.c_str(), BIN_FLAGS_WRITEABLE | BIN_FLAGS_HASH_DATA); pqiarchive *pa_out = new pqiarchive(rss, out, BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_DELETE); @@ -312,39 +317,25 @@ bool p3MsgService::loadConfiguration(std::string &loadHash) std::string msgfile = Filename(); RsSerialiser *rss = new RsSerialiser(); - rss->addSerialType(new RsMsgSerialiser()); + rss->addSerialType(new RsMsgSerialiser(true)); // create serialiser for configuration BinFileInterface *in = new BinFileInterface(msgfile.c_str(), BIN_FLAGS_READABLE | BIN_FLAGS_HASH_DATA); pqiarchive *pa_in = new pqiarchive(rss, in, BIN_FLAGS_READABLE); RsItem *item; RsMsgItem *mitem; + std::list items; + // load items and calculate next unique msgId while((item = pa_in -> GetItem())) { if (NULL != (mitem = dynamic_cast(item))) { - /* switch depending on the PENDING - * flags - */ /* STORE MsgID */ - mitem->msgId = getNewUniqueMsgId(); - if (mitem -> msgFlags & RS_MSG_FLAGS_PENDING) - { - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - - //std::cerr << "MSG_PENDING"; - //std::cerr << std::endl; - //mitem->print(std::cerr); - - msgOutgoing[mitem->msgId] = mitem; - } - else - { - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - - imsg[mitem->msgId] = mitem; + if (mitem->msgId >= mMsgUniqueId) { + mMsgUniqueId = mitem->msgId + 1; } + items.push_back(mitem); } else { @@ -352,9 +343,41 @@ bool p3MsgService::loadConfiguration(std::string &loadHash) } } + // sort items into lists + std::list::iterator it; + for (it = items.begin(); it != items.end(); it++) + { + mitem = *it; + + /* STORE MsgID */ + if (mitem->msgId == 0) { + mitem->msgId = getNewUniqueMsgId(); + } + + /* switch depending on the PENDING + * flags + */ + if (mitem -> msgFlags & RS_MSG_FLAGS_PENDING) + { + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ + + //std::cerr << "MSG_PENDING"; + //std::cerr << std::endl; + //mitem->print(std::cerr); + + msgOutgoing[mitem->msgId] = mitem; + } + else + { + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ + + imsg[mitem->msgId] = mitem; + } + } + std::string hashin = in->gethash(); - delete pa_in; + delete pa_in; if (hashin != loadHash) { diff --git a/libretroshare/src/util/rsdir.cc b/libretroshare/src/util/rsdir.cc index 45e9f5d06..17708465d 100644 --- a/libretroshare/src/util/rsdir.cc +++ b/libretroshare/src/util/rsdir.cc @@ -30,6 +30,7 @@ #include #include "util/rsdir.h" +#include "pqi/pqinotify.h" #include #include #include @@ -428,6 +429,60 @@ bool RsDirUtil::renameFile(const std::string& from, const std::string& to) return true ; } +bool RsDirUtil::createBackup (std::string sFilename, unsigned int nCount) +{ +#ifdef WINDOWS_SYS + if (GetFileAttributes (sFilename.c_str ()) == -1) { + // file doesn't exist + return true; + } + + // search last backup + int nLast; + for (nLast = nCount; nLast >= 1; nLast--) { + std::ostringstream sStream; + sStream << sFilename << nLast << ".bak"; + + if (GetFileAttributes (sStream.str ().c_str ()) != -1) { + break; + } + } + + // delete max backup + if (nLast == nCount) { + std::ostringstream sStream; + sStream << sFilename << nCount << ".bak"; + if (DeleteFile (sStream.str ().c_str ()) == FALSE) { + getPqiNotify()->AddSysMessage (0, RS_SYS_WARNING, "File delete error", "Error while deleting file " + sStream.str ()); + return false; + } + nLast--; + } + + // rename backups + for (int nIndex = nLast; nIndex >= 1; nIndex--) { + std::ostringstream sStream; + sStream << sFilename << nIndex << ".bak"; + std::ostringstream sStream1; + sStream1 << sFilename << nIndex + 1 << ".bak"; + + if (renameFile (sStream.str (), sStream1.str ()) == false) { + getPqiNotify()->AddSysMessage (0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + sStream.str () + " to " + sStream1.str ()); + return false; + } + } + + // copy backup + std::ostringstream sStream; + sStream << sFilename << 1 << ".bak"; + if (CopyFile (sFilename.c_str (), sStream.str ().c_str (), FALSE) == FALSE) { + getPqiNotify()->AddSysMessage (0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + sFilename + " to " + sStream.str ()); + return false; + } +#endif + return true; +} + #if 0 // NOT ENABLED YET! /************************* WIDE STRING ***************************/ /************************* WIDE STRING ***************************/ diff --git a/libretroshare/src/util/rsdir.h b/libretroshare/src/util/rsdir.h index 25a00431b..ffc824bda 100644 --- a/libretroshare/src/util/rsdir.h +++ b/libretroshare/src/util/rsdir.h @@ -44,6 +44,7 @@ std::string removeRootDirs(std::string path, std::string root); // Renames file from to file to. Files should be on the same file system. // returns true if succeed, false otherwise. bool renameFile(const std::string& from,const std::string& to) ; +bool createBackup (std::string sFilename, unsigned int nCount = 5); int breakupDirList(std::string path, std::list &subdirs); diff --git a/retroshare-gui/src/gui/MessagesDialog.cpp b/retroshare-gui/src/gui/MessagesDialog.cpp index 2609e0e39..2c9520c9e 100644 --- a/retroshare-gui/src/gui/MessagesDialog.cpp +++ b/retroshare-gui/src/gui/MessagesDialog.cpp @@ -230,9 +230,7 @@ MessagesDialog::MessagesDialog(QWidget *parent) connect( ui.messagestreeView, SIGNAL(clicked ( const QModelIndex &) ) , this, SLOT( clicked( const QModelIndex & ) ) ); connect( ui.messagestreeView, SIGNAL(doubleClicked ( const QModelIndex& ) ) , this, SLOT( doubleClicked( const QModelIndex & ) ) ); connect( ui.listWidget, SIGNAL( currentRowChanged ( int) ), this, SLOT( changeBox ( int) ) ); -#ifdef STATIC_MSGID connect( ui.tagWidget, SIGNAL( currentRowChanged ( int) ), this, SLOT( changeTag ( int) ) ); -#endif connect(ui.newmessageButton, SIGNAL(clicked()), this, SLOT(newmessage())); connect(ui.removemessageButton, SIGNAL(clicked()), this, SLOT(removemessage())); @@ -366,15 +364,8 @@ MessagesDialog::MessagesDialog(QWidget *parent) updateMessageSummaryList(); ui.listWidget->setCurrentRow(ROW_INBOX); -#ifdef STATIC_MSGID // create tag menu fillTags(); -#else - ui.tagButton->setHidden(true); - - ui.Tags_Button->setHidden(true); - ui.tagWidget->setHidden(true); -#endif // create timer for navigation timer = new QTimer(this); @@ -424,11 +415,9 @@ void MessagesDialog::processSettings(bool bLoad) // state of message tree msgwheader->restoreState(Settings->value("MessageTree").toByteArray()); -#ifdef STATIC_MSGID // state of tag list bValue = Settings->value("tagList", true).toBool(); ui.Tags_Button->setChecked(bValue); -#endif // state of splitter ui.msgSplitter->restoreState(Settings->value("Splitter").toByteArray()); @@ -439,10 +428,8 @@ void MessagesDialog::processSettings(bool bLoad) // state of message tree Settings->setValue("MessageTree", msgwheader->saveState()); -#ifdef STATIC_MSGID // state of tag list Settings->setValue("tagList", ui.Tags_Button->isChecked()); -#endif // state of splitter Settings->setValue("Splitter", ui.msgSplitter->saveState()); @@ -454,7 +441,6 @@ void MessagesDialog::processSettings(bool bLoad) m_bProcessSettings = false; } -#ifdef STATIC_MSGID static void getMessageTags (RSettings *pConfig, QString msgId, QList &tagIds) { pConfig->beginGroup(CONFIG_SECTION_TAG); @@ -699,7 +685,6 @@ void MessagesDialog::fillTags() updateMessageSummaryList(); } -#endif // replaced by shortcut //void MessagesDialog::keyPressEvent(QKeyEvent *e) @@ -776,7 +761,6 @@ void MessagesDialog::messageslistWidgetCostumPopupMenu( QPoint point ) QList RowsUnread; int nCount = getSelectedMsgCount (NULL, &RowsRead, &RowsUnread); -#ifdef STATIC_MSGID QAction *markAsRead = new QAction(QIcon(":/images/message-mail-read.png"), tr( "Mark as read" ), this); connect(markAsRead , SIGNAL(triggered()), this, SLOT(markAsRead())); contextMnu.addAction(markAsRead); @@ -796,7 +780,6 @@ void MessagesDialog::messageslistWidgetCostumPopupMenu( QPoint point ) // add tags contextMnu.addMenu(ui.tagButton->menu()); contextMnu.addSeparator(); -#endif QAction *removemsgAct; if (nCount > 1) { @@ -1159,7 +1142,6 @@ void MessagesDialog::changeBox(int) m_bInChange = false; } -#ifdef STATIC_MSGID void MessagesDialog::changeTag(int) { if (m_bInChange) { @@ -1179,7 +1161,6 @@ void MessagesDialog::changeTag(int) m_bInChange = false; } -#endif static void InitIconAndFont(RSettings *pConfig, QStandardItem *pItem [COLUMN_COUNT], int nFlag) { @@ -1208,7 +1189,6 @@ static void InitIconAndFont(RSettings *pConfig, QStandardItem *pItem [COLUMN_COU } } -#ifdef STATIC_MSGID // show the locale "New" state if (bNew == false) { // check locale config @@ -1222,7 +1202,6 @@ static void InitIconAndFont(RSettings *pConfig, QStandardItem *pItem [COLUMN_COU } else { pItem[COLUMN_READ]->setIcon(QIcon(":/images/message-mail-state-read.png")); } -#endif // set font for (int i = 0; i < COLUMN_COUNT; i++) { @@ -1258,9 +1237,7 @@ void MessagesDialog::insertMessages() unsigned int msgbox = 0; bool bTrash = false; bool bFill = true; -#ifdef STATIC_MSGID int nTagId = 0; -#endif switch (m_eListMode) { case LIST_NOTHING: @@ -1303,7 +1280,6 @@ void MessagesDialog::insertMessages() } break; -#ifdef STATIC_MSGID case LIST_TAG: { QListWidgetItem *pItem = ui.tagWidget->currentItem(); @@ -1314,7 +1290,6 @@ void MessagesDialog::insertMessages() } } break; -#endif default: bFill = false; @@ -1327,10 +1302,8 @@ void MessagesDialog::insertMessages() } if (bFill) { -#ifdef STATIC_MSGID std::map TagItems; getTagItems(TagItems); -#endif /* search messages */ std::list msgToShow; @@ -1348,14 +1321,12 @@ void MessagesDialog::insertMessages() continue; } } -#ifdef STATIC_MSGID } else if (m_eListMode == LIST_TAG) { QList tagIds; getMessageTags (m_pConfig, QString::fromStdString(it->msgId), tagIds); if (qFind(tagIds.begin(), tagIds.end(), nTagId) == tagIds.end()) { continue; } -#endif } else { continue; } @@ -1516,7 +1487,6 @@ void MessagesDialog::insertMessages() // Init icon and font InitIconAndFont(m_pConfig, item, it->msgflags); -#ifdef STATIC_MSGID // Tags QList tagIds; getMessageTags (m_pConfig, msgId, tagIds); @@ -1540,7 +1510,6 @@ void MessagesDialog::insertMessages() for (int i = 0; i < COLUMN_COUNT; i++) { item[i]->setForeground(Brush); } -#endif // No of Files. { @@ -1579,18 +1548,10 @@ void MessagesDialog::insertMessages() ui.messagestreeView->showColumn(COLUMN_ATTACHEMENTS); ui.messagestreeView->showColumn(COLUMN_SUBJECT); -#ifdef STATIC_MSGID ui.messagestreeView->showColumn(COLUMN_READ); -#else - ui.messagestreeView->hideColumn(COLUMN_READ); -#endif ui.messagestreeView->showColumn(COLUMN_FROM); ui.messagestreeView->showColumn(COLUMN_DATE); -#ifdef STATIC_MSGID ui.messagestreeView->showColumn(COLUMN_TAGS); -#else - ui.messagestreeView->hideColumn(COLUMN_TAGS); -#endif ui.messagestreeView->hideColumn(COLUMN_SRCID); ui.messagestreeView->hideColumn(COLUMN_MSGID); ui.messagestreeView->hideColumn(COLUMN_CONTENT); @@ -1685,16 +1646,12 @@ void MessagesDialog::setMsgAsReadUnread(const QList &Rows, bool bRead) m_pConfig->beginGroup(CONFIG_SECTION_UNREAD); if (bRead) { // set as read in config -#ifdef STATIC_MSGID m_pConfig->setValue(mid, false); -#endif // set message to read rsMsgs->MessageRead(mid.toStdString()); } else { // set as unread in config -#ifdef STATIC_MSGID m_pConfig->setValue(mid, true); -#endif } m_pConfig->endGroup(); @@ -1702,7 +1659,6 @@ void MessagesDialog::setMsgAsReadUnread(const QList &Rows, bool bRead) } } -#ifdef STATIC_MSGID void MessagesDialog::markAsRead() { QList RowsUnread; @@ -1720,7 +1676,6 @@ void MessagesDialog::markAsUnread() setMsgAsReadUnread (RowsRead, false); updateMessageSummaryList(); } -#endif void MessagesDialog::insertMsgTxtAndFiles(QModelIndex Index, bool bSetToRead) { @@ -2208,13 +2163,10 @@ void MessagesDialog::updateMessageSummaryList() rsMsgs->getMessageSummaries(msgList); -#ifdef STATIC_MSGID QMap tagCount; -#endif /*calculating the new messages*/ for (it = msgList.begin(); it != msgList.end(); it++) { -#ifdef STATIC_MSGID /* calcluate tag count */ QList tagIds; getMessageTags (m_pConfig, QString::fromStdString(it->msgId), tagIds); @@ -2223,7 +2175,6 @@ void MessagesDialog::updateMessageSummaryList() nCount++; tagCount [*tagId] = nCount; } -#endif /* calculate box */ if (it->msgflags & RS_MSG_TRASH) { @@ -2238,13 +2189,11 @@ void MessagesDialog::updateMessageSummaryList() newInboxCount++; } else { // check locale config -#ifdef STATIC_MSGID m_pConfig->beginGroup(CONFIG_SECTION_UNREAD); if (m_pConfig->value(QString::fromStdString(it->msgId), false).toBool()) { newInboxCount++; } m_pConfig->endGroup(); -#endif } break; case RS_MSG_OUTBOX: @@ -2345,7 +2294,6 @@ void MessagesDialog::updateMessageSummaryList() textItem = tr("Total Sent:") + " " + QString::number(newSentboxCount); ui.totalSentbox_label->setText(textItem); -#ifdef STATIC_MSGID /* set tag counts */ int nRowCount = ui.tagWidget->count(); for (int nRow = 0; nRow < nRowCount; nRow++) { @@ -2359,7 +2307,6 @@ void MessagesDialog::updateMessageSummaryList() pItem->setText(sText); } -#endif } /** clear Filter **/ @@ -2369,7 +2316,6 @@ void MessagesDialog::clearFilter() ui.filterPatternLineEdit->setFocus(); } -#ifdef STATIC_MSGID void MessagesDialog::tagAboutToShow() { // activate actions from the first selected row @@ -2490,4 +2436,3 @@ void MessagesDialog::tagTriggered(QAction *pAction) } } } -#endif diff --git a/retroshare-gui/src/gui/MessagesDialog.h b/retroshare-gui/src/gui/MessagesDialog.h index 6d7f953c9..f44b71f22 100644 --- a/retroshare-gui/src/gui/MessagesDialog.h +++ b/retroshare-gui/src/gui/MessagesDialog.h @@ -37,9 +37,6 @@ class RSettings; -// Thunder: need a static msgId -//#define STATIC_MSGID - class MessagesDialog : public MainPage { Q_OBJECT @@ -50,11 +47,9 @@ public: /** Default Destructor */ ~MessagesDialog(); -#ifdef STATIC_MSGID static void initStandardTagItems(std::map &Items); void getTagItems(std::map &Items); void setTagItems(std::map &Items); -#endif // replaced by shortcut // virtual void keyPressEvent(QKeyEvent *) ; @@ -71,9 +66,7 @@ private slots: void msgfilelistWidgetCostumPopupMenu(QPoint); void changeBox( int newrow ); -#ifdef STATIC_MSGID void changeTag( int newrow ); -#endif void updateCurrentMessage(); void currentChanged(const QModelIndex&); void clicked(const QModelIndex&); @@ -94,10 +87,8 @@ private slots: void removemessage(); void undeletemessage(); -#ifdef STATIC_MSGID void markAsRead(); void markAsUnread(); -#endif void anchorClicked (const QUrl &); @@ -117,10 +108,8 @@ private slots: void filterColumnChanged(); void clearFilter(); -#ifdef STATIC_MSGID void tagTriggered(QAction *pAction); void tagAboutToShow(); -#endif private: class LockUpdate @@ -153,19 +142,12 @@ private: void processSettings(bool bLoad); void setToolbarButtonStyle(Qt::ToolButtonStyle style); -#ifdef STATIC_MSGID void fillTags(); -#endif bool m_bProcessSettings; bool m_bInChange; int m_nLockUpdate; // use with LockUpdate - enum { LIST_NOTHING, - LIST_BOX, -#ifdef STATIC_MSGID - LIST_TAG -#endif - } m_eListMode; + enum { LIST_NOTHING, LIST_BOX, LIST_TAG } m_eListMode; std::string mCurrCertId; std::string mCurrMsgId; diff --git a/retroshare-gui/src/gui/settings/MessagePage.cpp b/retroshare-gui/src/gui/settings/MessagePage.cpp index 8019cde70..4a7a87f44 100644 --- a/retroshare-gui/src/gui/settings/MessagePage.cpp +++ b/retroshare-gui/src/gui/settings/MessagePage.cpp @@ -59,12 +59,10 @@ MessagePage::save(QString &errmsg) { Settings->setMsgSetToReadOnActivate(ui.setMsgToReadOnActivate->isChecked()); -#ifdef STATIC_MSGID MessagesDialog *pPage = (MessagesDialog*) MainWindow::getPage (MainWindow::Messages); if (pPage) { pPage->setTagItems (m_TagItems); } -#endif return true; } @@ -75,7 +73,6 @@ MessagePage::load() { ui.setMsgToReadOnActivate->setChecked(Settings->getMsgSetToReadOnActivate()); -#ifdef STATIC_MSGID MessagesDialog *pPage = (MessagesDialog*) MainWindow::getPage (MainWindow::Messages); if (pPage) { pPage->getTagItems (m_TagItems); @@ -90,13 +87,6 @@ MessagePage::load() ui.deletepushButton->setEnabled(false); ui.defaultTagButton->setEnabled(false); } -#else - ui.tags_listWidget->setEnabled(false); - ui.addpushButton->setEnabled(false); - ui.editpushButton->setEnabled(false); - ui.deletepushButton->setEnabled(false); - ui.defaultTagButton->setEnabled(false); -#endif } // fill items @@ -173,9 +163,7 @@ void MessagePage::deleteTag() void MessagePage::defaultTag() { -#ifdef STATIC_MSGID MessagesDialog::initStandardTagItems(m_TagItems); -#endif fillTagItems(); }