Author: jerome
Date: 2009-10-06 12:04:24 +0200 (Tue, 06 Oct 2009)
New Revision: 5601
Modified:
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/Configuration.java
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/TwitterPlugin.java
Log:
* Getting privates / public messages and friends updates instead of saying
sentenses.
* Code cleanup.
Modified:
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/Configuration.java
===================================================================
---
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/Configuration.java
2009-10-05 13:13:03 UTC (rev 5600)
+++
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/Configuration.java
2009-10-06 10:04:24 UTC (rev 5601)
@@ -1,5 +1,3 @@
-import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePluginConfiguration;
-
/* This file is part of "Tux Droid Gadget Twitter".
* Copyright 2008, kysoh
* Author : Jerome Conan
@@ -22,16 +20,15 @@
*/
+import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePluginConfiguration;
+
public class Configuration extends SimplePluginConfiguration{
private String username = "";
private String password = "";
private String myStatus = "Hello all from my tuxdroid Twitter gadget";
private boolean updateStatus = true;
- private String maxRead = "10";
- private boolean giveMessages = true;
- private boolean giveOtherReplies = true;
/**
@@ -111,64 +108,5 @@
{
return this.updateStatus;
}
-
-
- /**
- * Set giveMessages parameter.
- * @param giveMessages
- */
- public void setGiveMessages(boolean giveMessages)
- {
- this.giveMessages = giveMessages;
- }
-
-
- /**
- * Return true if user want to hear his messages.
- * @return
- */
- public boolean getGiveMessages()
- {
- return this.giveMessages;
- }
-
-
- /**
- * Return true if users want the heard replies giver by different user
to an other one.
- * @return
- */
- public boolean getGiveOtherReplies()
- {
- return this.giveOtherReplies;
- }
-
-
- /**
- * Set 'give other replies' option.
- * @param give
- */
- public void setGiveOtherReplies(boolean give)
- {
- this.giveOtherReplies = give;
- }
-
-
- /**
- * Set the maximum entries to read.
- * @param value
- */
- public void setMaxRead(String value)
- {
- this.maxRead = value;
- }
-
-
- /**
- * Return the maximum entries allowed to be read.
- * @return
- */
- public int getMaxRead()
- {
- return Integer.valueOf(this.maxRead).intValue();
- }
+
}
Modified:
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/TwitterPlugin.java
===================================================================
---
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/TwitterPlugin.java
2009-10-05 13:13:03 UTC (rev 5600)
+++
software_suite_v3/software/plugin/plugin-twitter/branches/october_release/plugin-twitter/src/TwitterPlugin.java
2009-10-06 10:04:24 UTC (rev 5601)
@@ -1,29 +1,26 @@
-/* This file is part of "Tux Droid Gadget Twitter".
+/* This file is part of "Tux Droid Plugin Twitter".
* Copyright 2008, kysoh
* Author : Jerome Conan
* Site : http://www.kysoh.com/
*
- * "Tux Droid Gadget Twitter" is free software; you can redistribute it and/or
+ * "Tux Droid Plugin Twitter" is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
- * "Tux Droid Gadget Twitter" is distributed in the hope that it will be
useful,
+ * "Tux Droid Plugin Twitter" is distributed in the hope that it will be
useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
- * License along with "Tux Droid Gadget Twitter"; if not, write to the Free
+ * License along with "Tux Droid Plugin Twitter"; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import java.util.List;
-import java.util.Vector;
-
import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePlugin;
-
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import winterwell.jtwitter.Twitter.Message;
@@ -33,41 +30,27 @@
public class TwitterPlugin extends SimplePlugin<Configuration>{
- private TwitterDatasStruct savedStruct;
+ private static TwitterDatasStruct savedStruct;
- private boolean isCheck = false;
- private boolean throwed = false;
- private String[] smileys = new String[]{":)", ":-)", ":(", ":-(", ":|",
":-|",
- ":$", ":-$", ";)",
";-)", ";(", ";-(",
- "o/", "\\o", "\\o/",
":o", ":-o", ":@",
- ":-@", ":D", ":-D",
":p", ":d", ":-p",
- ":-d", "=)", "=-)",
"$)", "$-)"};
-
- @Override
- protected void start() throws Throwable
+ /***
+ This function set the new user status.
+ Préconditions :
+ - To be in run mode
+ - Activate targeted option.
+ ***/
+ @SuppressWarnings("unused")
+ private boolean setUserStatus(Twitter twitter)
{
- if(this.getCommand().equalsIgnoreCase("check"))
+ if(this.getCommand().equalsIgnoreCase("run"))
{
- this.isCheck = true;
- }
-
- savedStruct = new
TwitterDatasStruct(this.configuration().getUsername());
- // Getting connected to twitter.
- Twitter twitter = null;
- twitter = new Twitter(this.configuration().getUsername(),
this.configuration().getPassword());
-
-
- twitter.setSource("tuxdroid");
-
- if(!this.isCheck)
- {
//Set user status
if(this.configuration().getUpdateStatus())
{
try
{
twitter.updateStatus(this.configuration().getMyStatus());
+ return true;
}
catch(TwitterException notConnected)
{
@@ -77,254 +60,170 @@
}
}
- Vector<Twitter.Status> repliesToOtherUsers = new
Vector<Twitter.Status>();
+ return false;
+ }
+
+
+ /***
+ * Get all new friend statuses.
+ * Automatic option.
+ ***/
+ private int getNewFriendStatusesCount(Twitter twitter)
+ {
- try
- {
- boolean noStatuses = true;
- List<User> friend = twitter.getFriends();
+ int statusesCount = 0;
+ List<User> friend = twitter.getFriends();
- int max = 0;
-
- for(int i=0; i < twitter.getFriends().size(); i++)
- {
- try
- {
-
- if( max >=
this.configuration().getMaxRead() )
- {
- break;
- }
-
- Status status =
friend.get(i).getStatus();
+ for( int i = 0 ; i < friend.size() ; i++ )
+ {
+ try
+ {
+ Status status = friend.get(i).getStatus();
- String statusText = status.getText();
- String username = status.getUser().getName();
+ String statusText = status.getText();
+ String username = status.getUser().getName();
- //if a new status was set for this user.
- boolean said =
this.savedStruct.isRegistered(TwitterDatasStruct.FRIENDS_STATUSES, username,
statusText);
- if((statusText != "null") && !said)
- {
- noStatuses = false;
- String stat = statusText.replace("&",
"");
+ //if a new status was set for this user text.
+ boolean said =
savedStruct.isRegistered(TwitterDatasStruct.FRIENDS_STATUSES, username,
statusText);
+
+ if((statusText != "null") && !said)
+ {
+ String stat = statusText.replace("&", "");
+ stat = this.urlFilter(stat);
- for(String simley : this.smileys)
- {
- stat = stat.replace(simley, "");
- }
-
- stat = this.urlFilter(stat);
-
- if(isReplyToOtherUser(stat))
- {
- repliesToOtherUsers.add(status);
- }
-
- else
- {
- if(this.isCheck && (!throwed))
- {
- this.throwResult(true);
- this.throwed = true;
- }
- throwMessage(username + ", " +
stat);
- max += 1;
- }
-
+ if (! isReplyToOtherUser(stat))
+ {
+ statusesCount += 1;
//Saving last entry.
-
this.savedStruct.addFriendStatus(username, statusText);
- }
- }
- catch(NullPointerException statusNotExists)
- {
- ;
- }
+ savedStruct.addFriendStatus(username,
statusText);
+ }
+ }
}
-
-
- if((repliesToOtherUsers.size() > 0) &&
this.configuration().getGiveOtherReplies())
- {
- max = this.configuration().getMaxRead();
-
- for(Twitter.Status state : repliesToOtherUsers)
- {
- if( max <= 0 )
- {
- return;
- }
-
- String sender = state.getUser().getName();
- String to = state.getText().substring(1,
state.getText().indexOf(" "));
-
if(!to.equals(this.configuration().getUsername()))
- {
- String reply =
state.getText().substring(state.getText().indexOf(" ") + 1 );
- reply = this.urlFilter(reply);
-
- if(this.isCheck && (!throwed))
- {
- this.throwResult(true);
- this.throwed = true;
- }
-
- throwMessage("Reply from {0} to {0},
the reply is : {2}", sender, to, reply);
- }
-
- max -= 1;
- }
- }
-
-
- if(noStatuses)
+ catch(NullPointerException statusNotExists)
{
- if(!this.isCheck)
- {
- throwMessage("No new tweets
available.");
- }
+ //Simply pass in case of exception, friend
status change will not be counted.
+ ;
}
- }
- catch(TwitterException badRequest)
+
+ }
+
+ return statusesCount;
+ }
+
+
+
+ /***
+ * This function return how many messages user has into his private
Twitter mailbox.
+ * @return an integer that represent user privates messages count.
+ ***/
+ private int getPrivateMessagesCount(Twitter twitter)
+ {
+ int messagesCount = 0;
+ List<Message> messages = twitter.getDirectMessages();
+ if(messages.size() > 0)
{
- if(!this.isCheck)
+ try
{
- throwMessage("Twitter is busy for now, please,
try later.");
+ //then give messages.
+ for(Message message : messages)
+ {
+ String username =
message.getSender().getName();
+
+ boolean said =
savedStruct.isRegistered(TwitterDatasStruct.MESSAGES, username,
message.getText());
+
+ if((message.getText() != "null") && !said)
+ {
+ messagesCount += 1;
+ savedStruct.addMessage(username,
message.getText());
+ }
+ }
}
- this.quitGadget();
+ catch(NullPointerException pointerError)
+ {
+ ;
+ }
}
-
-
- //Getting provates and public messages.
- if(this.configuration().getGiveMessages())
+ return messagesCount;
+ }
+
+
+
+ /***
+ * This function return how many messages user has into his public
Twitter mailbox.
+ * @return an integer that represent user public messages count.
+ ***/
+ private int getPublicMessagesCount(Twitter twitter)
+ {
+ int repliesCount = 0;
+ List<Message> replies = twitter.getReplies();
+
+ if(replies.size() > 0)
{
- boolean noReplies = true;
- boolean noMessage = true;
-
try
- {
- List<Message> replies = twitter.getReplies();
- List<Message> messages =
twitter.getDirectMessages();
-
- //Getting replies first.
- if(replies.size() > 0)
- {
- int maxRead =
this.configuration().getMaxRead();
-
- //then give replies.
- for(Message reply : replies)
- {
- boolean can = maxRead > 0 ? true :
false;
+ {
+ for(Message reply : replies)
+ {
+ String username = reply.getSender().getName();
- if(!can)
- {
- break;
- }
+ boolean said =
savedStruct.isRegistered(TwitterDatasStruct.REPLIES, username, reply.getText());
- String username =
reply.getSender().getName();
-
- boolean said =
this.savedStruct.isRegistered(TwitterDatasStruct.REPLIES, username,
reply.getText());
-
- if((reply.getText() != "null") && !said)
- {
- noReplies = false;
- String repl =
reply.getText().replace("&", "").replace("@" +
this.configuration().getUsername(), "");
-
- for(String simley :
this.smileys)
- {
- repl =
repl.replace(simley, "");
- }
-
- repl = this.urlFilter(repl);
-
- if(this.isCheck && (!throwed))
- {
- this.throwResult(true);
- this.throwed = true;
- }
-
- throwMessage("reply from {0},
the reply is: {1}", username, repl);
-
this.savedStruct.addReply(username, reply.getText());
- }
-
- maxRead -= 1;
+ if((reply.getText() != "null") && !said)
+ {
+ repliesCount += 1;
+ savedStruct.addReply(username,
reply.getText());
}
- }
-
- if(noReplies)
- {
- if(!this.isCheck)
- {
- throwMessage("you don't have
replies");
- }
- }
-
- if(messages.size() > 0)
- {
- int maxRead =
this.configuration().getMaxRead();
-
- //then give messages.
- for(Message message : messages)
- {
- boolean can = maxRead > 0 ?
true : false;
-
- if(!can)
- {
- break;
- }
-
- String username =
message.getSender().getName();
- boolean said =
this.savedStruct.isRegistered(TwitterDatasStruct.MESSAGES, username,
message.getText());
- if((message.getText() !=
"null") && !said)
- {
- noMessage = false;
- String mess =
message.getText().replace("&", "");
-
- for(String simley :
this.smileys)
- {
- mess =
mess.replace(simley, "");
- }
-
- mess =
this.urlFilter(mess);
-
- if(this.isCheck &&
(!throwed))
- {
- this.throwResult(true);
- this.throwed = true;
- }
-
- throwMessage("message
from {0}, the message is: {1}", username, mess);
-
this.savedStruct.addMessage(username, message.getText());
- }
-
- maxRead -=1;
- }
- }
-
- if(noMessage)
- {
- if(!this.isCheck)
- {
- throwMessage("you don't have
private messages");
- }
- }
+
+ }
}
- catch(TwitterException badRequest)
+ catch(NullPointerException pointerError)
+ {
+ ;
+ }
+ }
+ return repliesCount;
+ }
+
+
+
+ @Override
+ protected void start() throws Throwable
+ {
+ //Creating / getting sav context.
+ savedStruct = new
TwitterDatasStruct(this.configuration().getUsername());
+
+ /***
+ * Getting connected to twitter and setting update source (
here TuxDroid ).
+ ***/
+ Twitter twitter = null;
+ twitter = new Twitter(this.configuration().getUsername(),
this.configuration().getPassword());
+ twitter.setSource("tuxdroid");
+
+ //Getting Twitter informations.
+ try
+ {
+ throwMessage("New Friend updates: " +
String.valueOf(getNewFriendStatusesCount(twitter)));
+ throwMessage("New Replies: " +
String.valueOf(getPublicMessagesCount(twitter)));
+ throwMessage("New Messages: " +
String.valueOf(getPrivateMessagesCount(twitter)));
+ }
+ catch(TwitterException badRequest)
+ {
+ if ( ! this.getCommand().equalsIgnoreCase("check") )
{
- if(!this.isCheck)
- {
- throwMessage("Twitter is busy for now,
please, try later.");
- }
- this.quitGadget();
+ throwMessage("Twitter is busy for now, please,
try later.");
}
+
+ this.quitGadget();
}
- //RegisterStruct.
- this.savedStruct.registerStruct();
+ //Registering couted messages.
+ //savedStruct.registerStruct();
}
-
-
+
+
/**
* Return true if the text value indicates that the message was sent
from a user
- * to an other user , but not the current connected one.
+ * to an other user , but not the TuxDroid owner.
* @param value
* @return
*/
@@ -338,6 +237,11 @@
}
+ /***
+ * Remove Url's from text.
+ * @param string
+ * @return
+ ***/
public String urlFilter(String string)
{
while(string.contains("http://"))
@@ -399,7 +303,7 @@
*/
public void quitGadget()
{
- if(this.isCheck && this.throwed)
+ if(this.getCommand().equalsIgnoreCase("check") && this.throwed)
{
this.throwResult(false);
}
@@ -414,15 +318,15 @@
@Override
- protected void onPluginEvent(String arg0, String[] arg1) throws
Throwable {
- // TODO Auto-generated method stub
+ protected void onPluginEvent(String arg0, String[] arg1) throws
Throwable
+ {
;
}
@Override
- protected void onPluginStop() throws Throwable {
- // TODO Auto-generated method stub
+ protected void onPluginStop() throws Throwable
+ {
;
}
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn