Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Shikha_Pandey
Posts: 37
Registered: ‎07-13-2014
My Device: Z10

messages not getting written to xml file

Hi All,

 

I am writing messages of blackberry phone to a xml file. Problem which is happening is that all messages are being showed on GUI but when i write them to xml file only some are written many messages are missing .

 

Write code is as below:-

 

QFile myFile("/accounts/1000/removable/sdcard/messages/messages.xml");

	if(myFile.open(QIODevice::WriteOnly)) {
		QVariantList messageList = QVariantList();
		XmlDataAccess xda;
		QDataStream out(&myFile);

		// Iterate through all the messages and convert it into data stream
		foreach (const Message &message, inboxMessages) {

			QVariantMap messageMap = QVariantMap();

			//Adding sender Details to QVariantMap.
			messageMap["senderName"] = QVariant(message.sender().name());
			messageMap["addressSender"] = QVariant(message.sender().address());

						// Other details of message like body,text etc.
			messageMap["date"] = QVariant(message.serverTimestamp().toString("dd/MM/yyyy h:m:s AP"));
			messageMap["subject"] = QVariant(message.subject());
			messageMap["body"] = QVariant(message.body(MessageBody::PlainText).plainText());

			// Adding this object to Qvariant List
			messageList << QVariant(messageMap);
			messageMap.clear();

		}
		QVariantMap topLevelMessageMap;
		topLevelMessageMap[".root"] = QVariant("messages");
		topLevelMessageMap["message"] = QVariant(messageList);

		QVariant messageData = QVariant(topLevelMessageMap);
		xda.save(messageData, &myFile);

 

now messages.xml file which is created is having very few messages not all . iTs getting created of 1MB only.

 

Please help, i shalle be highly obliged.

 

Thanks & Regards,

Developer
BBSJdev
Posts: 6,116
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: messages not getting written to xml file

When you say all messages show in the GUI do you mean for your app or in the hub?

 

Have you tried putting a simple debug line in making sure that inboxMessages contains everything you think it does?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
scrudrv
Posts: 46
Registered: ‎11-23-2010
My Device: Q10

Re: messages not getting written to xml file

Your code looks ok, I would only replace these lines:

QVariantList messageList = QVariantList();
...
QVariantMap messageMap = QVariantMap();

 by

 

QVariantList messageList;
QVariantMap messageMap;

 But anyway, your current implementation will work and I tried it both writing to SD card and to local filesystem. I was using this code to create inboxMessages and populate messageList:

 

      for(int i = 0; i < 100; ++i)
      {
         QString senderName = "sender" + QString::number(i);
         QString addressSender = "address" + QString::number(i);

         QVariantMap messageMap = QVariantMap();
         messageMap["senderName"] = QVariant(senderName);
         messageMap["addressSender"] = QVariant(addressSender);
         messageList << QVariant(messageMap);

         messageMap.clear();
      }

 I think the problem is inboxMessages collection. It doesn't have all expected data initially.

 

New Developer
Shikha_Pandey
Posts: 37
Registered: ‎07-13-2014
My Device: Z10

Re: messages not getting written to xml file

Well thanks for replying ,

 

There are two problems which are happening now:-

 

1.) IN case of derserialization all messages are not getting derialized at once , i need to dereializing it in chunks of 100 and saving it using Message Service .save method

 

2.) There is date time issue.  When deserializing using first chuink of 100 date and time of messages ic coming correct but when de-serializing next 100 chunk of message date time is getting displayed as current date

 

Code is as follows:-

 

DESERIALIZING:-

 

bool Messages::deserializeMessages(const QString & filePath) {

	bool successFlag = false;
	QVariant parsedresponselist ;
	QVariantList qVariantList ;
	XmlDataAccess xda ;
	QFile myFile(filePath);

	if(myFile.open(QIODevice::ReadOnly)) {
		parsedresponselist = xda.load(&myFile,"/messages/message");
		qVariantList = parsedresponselist.toList();
		QList<Message> parsedMessageList = QList<Message>();
		foreach (QVariant listMember , qVariantList) {

			QVariantMap parsedMessageMap = QVariantMap();
			parsedMessageMap      = 	listMember.toMap();
			QString senderName    = 	parsedMessageMap["senderName"].toString();
			QString senderAddress = 	parsedMessageMap["senderAddress"].toString();
			QString subject       = 	parsedMessageMap["subject"].toString();
			QString messageBody   = 	parsedMessageMap["body"].toString();
			QString date          = 	parsedMessageMap["date"].toString();
			parsedMessageMap.clear();

			// starting code for building message

			MessageBuilder *builder = MessageBuilder::create(m_currentAccount.id());
			builder ->sender(MessageContact(m_currentAccount.id(), MessageContact::From,senderName,senderAddress));
			builder->body(MessageBody::PlainText,messageBody.toUtf8());
			builder->subject(subject);
			builder->deviceTimestamp(QDateTime::fromString(date,"ddd MMM dd hh:mm:ss yyyy"));
			builder->serverTimestamp(QDateTime::fromString(date,"ddd MMM dd hh:mm:ss yyyy"));

			Message buildmessage = *builder;
			delete builder;
			parsedMessageList.append(buildmessage);

		}

		// working with parsedMessageList which contains the messages parsed from the xml file.
		if (!parsedMessageList.isEmpty()) {
			//MessageFolderKey parsedMessageFolderKey;
			QString parsedMessageFolderName = "ParsedMessages";
			//MessageFolder parsedMessageFolder = new MessageFolder(inboxFolderKeyID,parsedMessageFolderKey,parsedMessageFolderName,MessageFolder::Type);
			// Calling save method will result the messages being saved in Draft folder.
			//m_messageService->addMessageFolder(m_currentAccount.id(),parsedMessageFolderKey,parsedMessageFolderName);
			foreach (const Message &message, parsedMessageList) {
				m_messageService->save(m_currentAccount.id(),message);
			}
			successFlag = true;
		}
		myFile.close();
	}
	toast("Blackberry Messages Parsed Successfully and saved to DRAFTS folder");
	return successFlag;
}

 

 

SERIALIZING:-

 

bool Messages::serializeMessages() {

	bool successFlag = false;
	QDir dir;
	bb::device::SdCardInfo sDCardInfo;
	if(sDCardInfo.state() == bb::device::SdCardState::Mounted)
	{
		dir.cd("/accounts/1000/removable/sdcard/");
		dir.mkdir("messages");
	}
	QFile myFile("/accounts/1000/removable/sdcard/messages/messages.xml");

	if(myFile.open(QIODevice::WriteOnly)) {
		QVariantList messageList = QVariantList();
		XmlDataAccess xda;
		QDataStream out(&myFile);

		// Iterate through all the messages and convert it into data stream
		foreach (const Message &message, inboxMessages) {

			QVariantMap messageMap = QVariantMap();

			//Adding sender Details to QVariantMap.
			if (message.sender().displayableName() == "") {
			    messageMap["senderName"] = QVariant(message.sender().name());
			}
			else {
			    messageMap["senderName"] = QVariant(message.sender().displayableName());
			}
			messageMap["addressSender"] = QVariant(message.sender().address());

			// Other details of message like body,text etc.
			messageMap["date"] = QVariant(message.serverTimestamp().toString("ddd MMM dd hh:mm:ss yyyy"));
			messageMap["subject"] = QVariant(message.subject());
			messageMap["body"] = QVariant(message.body(MessageBody::PlainText).plainText());

			// Adding this object to Qvariant List
			messageList << QVariant(messageMap);
			messageMap.clear();

		}
		QVariantMap topLevelMessageMap;
		topLevelMessageMap[".root"] = QVariant("messages");
		topLevelMessageMap["message"] = QVariant(messageList);

		QVariant messageData = QVariant(topLevelMessageMap);
		xda.save(messageData, &myFile);
		successFlag = true;
		myFile.close();
	}
	return successFlag;
}

 

MESSAGE COLLECTION RETRIEVAL

 

void Messages::filterMessages()
{
	if (!m_currentAccount.isValid())
		return;

	// Use the entered filter string as search value
	MessageSearchFilter filter;
	filter.addSearchCriteria(SearchFilterCriteria::Any, m_filter);

	MessageFilter messageFilter;
	// messageFilter.insert(MessageFilter::);

	inboxMessages = m_messageService->messages(m_currentAccount.id(),messageFilter);//searchLocal(m_currentAccount.id(), filter);

 

 

Please suggest what wrong is happening here and how to correct both these issues

 

Thanks & Regards