Offline message show as ???? , aSmack and openfire - offline

I use asmack and openfire for chat application and have two problem:
Problem with offline message encoding :
user A send message to user B(is offline)
B can received offline message but when user A use Persian character user B see message like '????'.
How to detect message was sent?
I use below code to set message as sent.
below code work when I have good Internet access but some time when use poor internet access, message set as sent but doesn't sent really.
try {
connection.sendPacket(msg);
db.setMessageState(id, MessageModel.STATE_SENT);
} catch (Exception e) {
db.setMessageState(id, MessageModel.STATE_ERROR);
}
Edit:
My first problem was solved, this link may be helpful.

configureConnection(ProviderManager.getInstance());
OfflineMessageManager offlineManager = new OfflineMessageManager(
connection);
int count = offlineManager.getMessageCount();
Iterator<Message> it = offlineManager.getMessages();
while (it.hasNext()) {
Message m = it.next();
String fromName = m.getFrom();
//Date currentDate = new Date();
String text = m.getBody() + " (offline)";
if(fromName.contains("/Smack")){
fromName = fromName.replace("/Smack","");
}
Log.i("offile chat message with user name ",text+" username "+fromName);
dbHelper.addmessageUser(fromName, text, "0", "1",""+System.currentTimeMillis());
if(offlineMsgs.containsKey(fromName))
{
offlineMsgs.get(fromName).add(m);
}else{
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(m);
offlineMsgs.put(fromName, temp);
}
// ...
}
offlineManager.deleteMessages();
//
public void configureConnection(ProviderManager pm) {
// Private Data Storage
pm.addIQProvider("query", "jabber:iq:private", new PrivateDataManager.PrivateDataIQProvider());
// Time
try
{
pm.addIQProvider("query", "jabber:iq:time", Class.forName("org.jivesoftware.smackx.packet.Time"));
}
catch (ClassNotFoundException e)
{
Log.w("TestClient", "Can't load class for org.jivesoftware.smackx.packet.Time");
}
// Roster Exchange
pm.addExtensionProvider("x", "jabber:x:roster", new RosterExchangeProvider());
// Message Events
pm.addExtensionProvider("x", "jabber:x:event", new MessageEventProvider());
// Chat State
pm.addExtensionProvider("active", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("composing", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("paused", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("inactive", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("gone", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
// XHTML
pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im", new XHTMLExtensionProvider());
// Group Chat Invitations
pm.addExtensionProvider("x", "jabber:x:conference", new GroupChatInvitation.Provider());
// Service Discovery # Items
pm.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
// Service Discovery # Info
pm.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
// Data Forms
pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
// MUC User
pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user", new MUCUserProvider());
// MUC Admin
pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin", new MUCAdminProvider());
// MUC Owner
pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner", new MUCOwnerProvider());
// Delayed Delivery
pm.addExtensionProvider("x", "jabber:x:delay", new DelayInformationProvider());
// Version
try
{
pm.addIQProvider("query", "jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
}
catch (ClassNotFoundException e)
{
// Not sure what's happening here.
}
// VCard
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
// Offline Message Requests
pm.addIQProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageRequest.Provider());
// Offline Message Indicator
pm.addExtensionProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageInfo.Provider());
// Last Activity
pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
// User Search
pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
// SharedGroupsInfo
pm.addIQProvider("sharedgroup", "http://www.jivesoftware.org/protocol/sharedgroup", new SharedGroupsInfo.Provider());
// JEP-33: Extended Stanza Addressing
pm.addExtensionProvider("addresses", "http://jabber.org/protocol/address", new MultipleAddressesProvider());
// FileTransfer
pm.addIQProvider("si", "http://jabber.org/protocol/si", new StreamInitiationProvider());
pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams", new BytestreamsProvider());
// Privacy
pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
pm.addIQProvider("command", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider());
pm.addExtensionProvider("malformed-action", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.MalformedActionError());
pm.addExtensionProvider("bad-locale", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadLocaleError());
pm.addExtensionProvider("bad-payload", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadPayloadError());
pm.addExtensionProvider("bad-sessionid", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadSessionIDError());
pm.addExtensionProvider("session-expired", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.SessionExpiredError());
}

Related

Why am I getting bad request from google?

My objective
To exchange refresh token for access token from google using OAuth 2.
My code
bool Google_Account::Refresh_Access_Token_Using_Refresh_Token()
{
// Prepare Url
QUrl url(tr("https://www.googleapis.com/oauth2/v4/token"));
// Create request
QNetworkRequest request(url);
request.setRawHeader("Host:","www.googleapis.com");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// Create request body ClientID, ClientSecret, RefreshTokenString are class data members
QString RequestBody = tr("client_secret=%1&").arg(ClientSecret) +
tr("grant_type=refresh_token&")+
tr("refresh_token=%1&").arg(RefreshTokenString)+
tr("client_id=%1").arg(ClientID);
QByteArray array = RequestBody.toUtf8();
// Get reply
QNetworkReply *reply = mQNAM.post(request, array); // mQNAM is QNetworkAccessManager
// Set timeout to reply while waiting for reply finished
bool stop = false;
QTimer timer;
timer.setSingleShot(true);
QObject::connect(&timer, &QTimer::timeout, [&](){
qDebug()<<"Time out";
stop = true;
});
timer.start(5000);
// Wait till the response is completed
while(!reply->isFinished()){
QCoreApplication::processEvents();
if(stop){
qDebug()<<"Going to abort";
reply->abort();
}
}
// Check for reply
if(reply->isFinished()){
if(reply->error() != QNetworkReply::NoError){
qDebug()<<reply->readAll();
emit setMessage("Error: "+reply->errorString());
delete reply;
return false;
}
else{
QByteArray array = reply->readAll();
QJsonDocument document = QJsonDocument::fromJson(array);
QJsonObject obj = document.object();
access_token = obj.value("access_token").toString(); //access_token is class data variable
delete reply;
return true;
}
}
else{
delete reply;
return false;
}
}
The problem is that if I run this code in my windows 7 pc(Qt 5.11.1) everything is fine I get the access token but if i run in my raspberry pi(raspbian Qt 5.7) I get Error 400, Bad request from google. I tried using the access_token got from my windows pc and made other request such as to get the file list from google drive, they are working fine in raspberry, but only this I am having problem. What am I doing wrong?
P.S the code is refactored to the specific details only, in reality I am getting the client id and other keys from QSettings
This smells like you're getting a 400 code when Qt is requesting a token, because the login code you get when the flow returns to your app is URL-encoded. We wrote about How To Authenticate with Google SSO in Qt with some code samples including this bit that injects a parameter modifier in the flow:
google->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
// Percent-decode the "code" parameter so Google can match it
if (stage == QAbstractOAuth::Stage::RequestingAccessToken) {
QByteArray code = parameters->value("code").toByteArray();
(*parameters)["code"] = QUrl::fromPercentEncoding(code);
}
});
Do that before the initial request, but as suggested by #Giancarlo above, I'd rewrite your code to work asynchronously. It's simpler, and more reliable.

How to close the card by itself after executed a HTTP request?

I have an app with contextual commands. After triggered a contextual command, it will make a HTTP request with a link and post the result on the card, something like, "Completed!". I want this card to be closed by itself after one second so that the user need not to tap to close it. Once the result card is closed, it will go back to contextual command lists with "Ok, glass" at footer and ready for next command.
May i know how to do that?
private class HTTPRequest extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
try {
if (mWhat.equalsIgnoreCase("GET")) {
// get json via YouTube API
URL url = new URL("http://example.com");
mUrlConnection = (HttpURLConnection)
url.openConnection();
InputStream in = new BufferedInputStream(
mUrlConnection.getInputStream());
int ch;
StringBuffer b = new StringBuffer();
while ((ch = in.read()) != -1) {
b.append((char) ch);
}
mResult = new String(b);
}
} catch (Exception e) {}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mTvInfo.setText(mResult);
}
You can use an Android Dialog for this:
Use CardBuilder to create the "Completed" card using the MENU layout.
Create a new instance of Dialog and set its content view to be the view returned by CardBuilder.getView.
Show the dialog.
Use Handler.postDelayed (or some similar mechanism) to automatically dismiss the dialog after the desired amount of time has passed.

BB10 - Sending SMS from cascades app

I am using the following code to send an SMS from my app;
void App::sendSms(const QString &messageText, const QStringList &phoneNumbers) {
bb::pim::account::AccountService accountService;
bb::pim::message::MessageService messageService;
QList<Account> accountListy = accountService.accounts(bb::pim::account::Service::Messages,"sms-mms");
bb::pim::account::AccountKey smsAccountId = 0;
if(!accountListy.isEmpty()) {
smsAccountId = accountListy.first().id();
qDebug() << "SMS-MMS account ID:" << smsAccountId;
}
else {
qWarning() << "Could not find SMS account";
return;
}
QList<bb::pim::message::MessageContact> participants;
foreach(const QString &phoneNumber, phoneNumbers) {
bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(
-1, bb::pim::message::MessageContact::To,
phoneNumber, phoneNumber);
participants.append(recipient);
}
bb::pim::message::ConversationBuilder *conversationBuilder =
bb::pim::message::ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
bb::pim::message::Conversation conversation = *conversationBuilder;
bb::pim::message::ConversationKey conversationId = messageService.save(smsAccountId, conversation);
bb::pim::message::MessageBuilder *builder =
bb::pim::message::MessageBuilder::create(smsAccountId);
builder->conversationId(conversationId);
builder->addAttachment(bb::pim::message::Attachment("text/plain", "", messageText.toUtf8()));
foreach(const bb::pim::message::MessageContact recipient, participants) {
builder->addRecipient(recipient);
}
bb::pim::message::Message message = *builder;
messageService.send(smsAccountId, message);
delete builder;
delete conversationBuilder;
}
However everytime it sends a new SMS, it creates a new thread in the Text Messages UI. I was wondering if there was a way to add the new message to the thread that already exists for the number it is going to send to?
Thanks!
The aspect of your code which causes this bug is
// at top of file
using namespace bb::pim::messages;
ConversationBuilder *conversationBuilder = ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
Conversation conversation = *conversationBuilder;
ConversationKey conversationId = messageService.save(smsAccountId, conversation);
This piece—following its preceeding lines, would create a new conversation for the participants regardless of previous existing conversations in the Hub.
To work around this, the BlackBerry Cascades PIM MessageService provides a MessageSearchFilter with which you can use to filter the conversations by any SearchFilterCriteria. Use it thus…
//Setup a filter
MessageFilter filter;
//Set our filter to filter conversations with id of the contact
filter.insert(MessageFilter::ContactId, contactId);
//Run filter
filterdConvosKeys = messageService.conversationKeys(smsAccountId, filter);
ConversationKey conversation_id;
//Vars for conversation builder
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);
//Get conversation ID for existing conversation, else create new
if (filterdConvosKeys.count() > 1) {
// go through all conversations for this contact and choose
// the conversation in which this contact is the sole participant
else if (filterdConvosKeys.count() == 1) {
conversation_id = filterdConvosKeys[0].toAscii();
} else {
conversation_id = messageService.save(smsAccountId, conversation);
}
Edit
Despite what the original Source says, I find it a bit buggy. If you use it exactly as it says, you will always end up with a new conversation if there is no conversation with the contact as the only participant. I tried doing a search in the BlackBerry Hub on my STL100-3 with the phone number of a contact, I ended up with many messages which were in the same conversation. But this means there is a possibility that many conversations are being returned if you filter by MessageFilter::Participants. It is better to filter using MessageFilter::ContactId.
p.s: I namespaced the code blocks so bb::pim::messages:: is not repeated.

Launch a fragment in my Android application from the notification bar

How do I start a fragment in my Android application from a notification in the notification bar?
I've tried to implement this answer of creating my own action and then setting the action to the intent, but I'm unsure how to use it and what is required additionally - like adding something to the Manifest.
I've got a notification class that receives a context, a message and then an action. I then want to filter on that action to determine which fragment to launch, but I don't know how to launch a fragment as opposed to launching an activity.
Here is my Notifications.java class (incomplete):
public class Notifications {
private Context mContext;
public Notifications(Context context) {
this.mContext = context;
}
public static void notify(Context context, String message, String action) {
//Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
action = "my.package.name.here.frag."+action;
//Construct a user message.
String appName = context.getResources().getString(R.string.app_name);
// Use the Notification manager to send notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a notification using android stat_notify_chat icon.
Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);
//Sound, lights, vibration.
//REMEMBER PERMISSIONS.
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Create a pending intent to open the application when the notification is clicked.
//Restart the app.
Intent launchIntent = null;
//Get the action and based on what the action is, launch the application displaying the appropriate fragment.
if (action.equalsIgnoreCase("friend")){
//New friend notification
//Launch application displaying the list of friends
}
if (action.equalsIgnoreCase("article")){
//New article has been posted
//Launch application displaying the news feed fragment
}
if (action.equalsIgnoreCase("points")){
//Points scored notification
//Launch application displaying the user's profile
}
if (action.equalsIgnoreCase("redeemable")){
//New redeemable is offered
//Launch application displaying the list of redeemables
}
if (!action.equalsIgnoreCase("friend")
&& !action.equalsIgnoreCase("article")
&& !action.equalsIgnoreCase("points")
&& !action.equalsIgnoreCase("redeemable")){
//Not specific, so launch the application from scratch displaying the activity feed
launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
}
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
}
So this was actually pretty easy. Hopefully I can help someone else see this too.
I send an action to this notify function. The I add that action to my intent to launch an activity. In my case I open the launching activity, because all the fragments are loaded from within that activity based on what the user does. So I set the action using setAction and the I use that action in the activity as below.
My Notifications.java class changed to this:
public static void notify(Context context, String message, String action) {
action = action.toUpperCase();
// Create a pending intent to open the the application when the notification is clicked.
//Restart the app.
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
And then in my activity from where I load the fragments, I get the action and filter it:
Intent intent = getIntent();
try{
String action = intent.getAction().toUpperCase();
if(action != null){
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_friend))){
goFrag(getResources().getInteger(R.integer.FRAG_A_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_article))){
goFrag(getResources().getInteger(R.integer.FRAG_B_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_points))){
goFrag(getResources().getInteger(R.integer.FRAG_C_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_redeemable))){
goFrag(getResources().getInteger(R.integer.FRAG_D_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_dance))){
goFrag(getResources().getInteger(R.integer.FRAG_E_INT));
}
}else{
Log.d(TAG, "Intent was null");
}
}catch(Exception e){
Log.e(TAG, "Problem consuming action from intent", e);
}
In my goFrag function I replace the fragment if the required fragment is still in memory (meaning the user was there earlier and it hasn't been destroyed yet), or I create a new fragment required.

Adding multiple QTreeWidgetItems

Im trying to build a QTreeWidget that has multiple QTreeWidgetItems, i tried to add some of them manually and it works. My question is how can i add the items using a for or a while loop.
here is a part of my code
Dwidget= new QDockWidget(this);
Dwidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
treeWidget= new QTreeWidget(Dwidget);
Titem= new QTreeWidgetItem(treeWidget);
Titem1= new QTreeWidgetItem();
Titem2= new QTreeWidgetItem();
Titem3= new QTreeWidgetItem();
Titem->setText(0,"WriterIdenSystem");
Titem->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/data_repository_icon.jpg")));
Titem1->setText(0,"Database for Writer Identification");
Titem1->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/card_file.png")));
Titem2->setText(0,"0001");
Titem2->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/Folder Open.png")));
Titem3->setText(0,"0002");
Titem3->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/Folder Open.png")));
Titem->addChild(Titem1);
Titem1->addChild(Titem2);
Titem1->addChild(Titem3);
treeWidget->addTopLevelItem(Titem);
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(on_actionRetrieve_Documents_triggered()));
Dwidget->setWidget(treeWidget);
addDockWidget(Qt::LeftDockWidgetArea,Dwidget);
Dwidget->show();
any ideas?? :)
According to your code, could could do something like this.
QTreeWidgetItem* items[ITEM_COUNT]; // Must be multiple of 3
QTreeWidget* treeWidget = new QTreeWidget;
if(!treeWidget){ /* operating system out of memory, handle the error */ }
for(int i=0;i<=(ITEM_COUNT-3);i+=3)
{
items[i ] = new QTreeWidgetItem(treeWidget);
if(!items[i]){ /* operating system out of memory, handle the error */ }
items[i+1] = new QTreeWidgetItem(treeWidget);
items[i+2] = new QTreeWidgetItem(treeWidget);
/* set text here */
items[i ]->addChild(items[i]);
items[i+1]->addChild(items[i+1);
items[i+1]->addChild(items[i+2);
treeWidget->addTopLevelItem(items[i]);
}