#!/usr/bin/python

# This is a part of the external Twitter applet for Cairo-Dock
#
# Author: Eduardo Mucelli Rezende Oliveira
# E-mail: edumucelli@gmail.com or eduardom@dcc.ufmg.br
#
# This program 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 3 of the License, or
#  (at your option) any later version.

# This program 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.

# This applet provides Cairo-Dock an interface with Twitter

# On the first time, the applet is going to ask your nickname and authorization to connect with Twitter.
# The applet is going to open your browser with the authorization page
# As soon as you authorize it, a PIN number will be shown on the page, copy this number
# Paste this number on the next dialog box will be shown.
# The plugin is going to inform that you are successfully connected.

# To see the received tweets right-click on the icon -> Twitter -> [New] tweets. You can retweet any of them by left-clicking on it.
# To see the received direct messages right-click on the icon -> Twitter -> [New] direct messages. You can reply any of them by left-clicking on it.
# To see some user's info right-click on the icon -> Twitter -> Info

import os, webbrowser, simplejson, threading, Queue, urllib2
from oauth import oauth
from util import *

from http import post, get #, stream
import emblem, menu
from message import DirectMessage, Tweet

from CDApplet import CDApplet, _
# TODO import ConfigParser later conver files to config syntax

class TwitterOauth:
  def __init__(self):
    self.request_token_url  = 'https://twitter.com/oauth/request_token'
    self.access_token_url   = 'https://twitter.com/oauth/access_token'
    self.authorize_url      = 'https://twitter.com/oauth/authorize'

    consumer_key, consumer_secret = read_consumer_key_and_secret()
    self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
    self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
    self.request_token = None
    self.access_token = None
    
  def get_authorization_url(self):
    self.request_token = self.get_unauthorized_request_token()
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer,
                                                               token = self.request_token,
                                                               http_url = self.authorize_url)
    oauth_request.sign_request(self.signature_method, self.consumer, self.request_token)
    return oauth_request.to_url()

  def get_unauthorized_request_token(self):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url = self.request_token_url)
    oauth_request.sign_request(self.signature_method, self.consumer, None)
    url = oauth_request.to_url()
    response = get(url)
    token = oauth.OAuthToken.from_string(response)
    return token

  # Exchange request token for access token
  def get_access_token_and_secret(self, pin):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer,
                                                               http_url = self.access_token_url,
                                                               verifier = pin,
                                                               token = self.request_token)
    oauth_request.sign_request(self.signature_method, self.consumer, self.request_token)
    url = oauth_request.to_url()
    response = get(url)
    self.access_token = oauth.OAuthToken.from_string(response)                      # create both .key and .secret attributes
    return self.access_token.key, self.access_token.secret
    
# TODO: Check also the possible inheritance with TwitterOauth
class API():
  def __init__(self, access_key, access_secret):
    self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
    consumer_key, consumer_secret = read_consumer_key_and_secret()
    self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
    self.access_token = oauth.OAuthToken(access_key, access_secret)

class TwitterStreamAPI(API):
  def __init__(self, access_key, access_secret, callback):
    API.__init__(self, access_key, access_secret)

    self.user_stream_url = "https://userstream.twitter.com/2/user.json"
    self.callback = callback                                                        # called as soon as a new entry on the stream appears
    thread = threading.Thread(target=self.tweet_streaming)                          # keep checking for new entries on the stream
    thread.start()                                                                  # run, forrest run

  def tweet_streaming(self):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer,
                                                               token = self.access_token,
                                                               http_url = self.user_stream_url)
    oauth_request.sign_request(self.signature_method, self.consumer, self.access_token)

    url = oauth_request.to_url()
    req = urllib2.urlopen(url)
   
    buffer = ''
    while True:
      chunk = req.read(1)                                               # read character per character from the connection ...
      if not chunk:
        break

      buffer += chunk
      tweets = buffer.split("\n",1)                                     # ... until find the end of a tweet marked with a '\n'
      if len(tweets) > 1:
        content = tweets[0]
        if "text" in content:
          content = simplejson.loads(content)
          logp("Received from Twitter Stream: %s" % content)
          self.callback(content)                                        # at the moment this method is called 'on_receive_new_entry_into_stream_callback'
        buffer = tweets[1]
 
class TwitterAPI(API):
  def __init__(self, access_key, access_secret):
    API.__init__(self, access_key, access_secret)
    
    self.update_url               = 'http://twitter.com/statuses/update.json'
    self.home_timeline_url        = 'http://twitter.com/statuses/home_timeline.json'
    self.direct_messages_url      = 'https://api.twitter.com/1/direct_messages.json'
    self.new_direct_messages_url  = 'https://api.twitter.com/1/direct_messages/new.json'
    self.verify_credentials_url   = 'https://api.twitter.com/1/account/verify_credentials.json'
    self.user_timeline_url        = 'http://api.twitter.com/1/statuses/user_timeline.json'
    self.retweet_url_prefix       = 'http://api.twitter.com/1/statuses/retweet/'            # lacks the id of the tweet to be retweeted
    
  def dispatch(self, url, mode, parameters={}):
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer,
                                                             token = self.access_token,
                                                             http_url = url,
                                                             parameters = parameters,
                                                             http_method = mode)
    oauth_request.sign_request(self.signature_method, self.consumer, self.access_token)
    if mode == "GET":
      url = oauth_request.to_url()
      response = get(url) 
      return simplejson.loads(response)
    elif mode == "POST":
      header = oauth_request.to_header()
      post(url, parameters, header)  
      
  def tweet(self, message):                                                                 # popularly "send a tweet"
    self.dispatch(self.update_url, "POST", {'status':message})
    
  def retweet(self, tweet_id):
    url = "%s%s.json" % (self.retweet_url_prefix, tweet_id)
    self.dispatch(url, "POST")

  def new_direct_message(self, message, destinatary):
    self.dispatch(self.new_direct_messages_url, "POST", {'text':message, 'screen_name':destinatary})

  def home_timeline(self):
    return self.dispatch(self.home_timeline_url, "GET")
  
  #TODO: Use it.
  def user_timeline(self):
    return self.dispatch(self.user_timeline_url, "GET")

  def direct_messages(self):
    return self.dispatch(self.direct_messages_url, "GET")

  def verify_credentials(self):
    return self.dispatch(self.verify_credentials_url, "GET")

class User:
  def __init__(self, screen_name="", access_key="", access_secret=""):
    self.screen_name = screen_name
    self.access_key = access_key
    self.access_secret = access_secret

class Applet(CDApplet):

  def inform_start_of_waiting_process(self):
    self.icon.SetQuickInfo("...")

  def inform_end_of_waiting_process(self):
    self.icon.SetQuickInfo("")

  def refresh_emblem_counter(self):
    counter = self.tweet_stream.qsize() + self.message_stream.qsize()
    if counter > 0:
      self.emblem.update(counter)                                                 # create the emblem with the counter
      self.icon.SetIcon(self.emblem.emblem)
    else:
      self.icon.SetIcon(os.path.abspath("./icon"))  
    
  def refresh_emblem_size(self):
    if self.config['emblem_size'] == "Small":
      self.emblem.set_size_small()
    elif self.config['emblem_size'] == "Medium":
      self.emblem.set_size_medium()
    else:
      self.emblem.set_size_large()

  def play_alert_sound(self):
    os.popen('aplay %s' % os.path.abspath("./data/alert.wav"))
  
  def animate_icon(self, animation):
    if len(animation) == 0:
      animation = 'default'
    self.icon.DemandsAttention(True, animation)

  def send_alert(self):
    if self.config['sound']:
      self.play_alert_sound()
    if self.config['animation']:
      self.animate_icon(self.config['animation_type'])

  # Twitter methods

  # This method is a callback that is called as soon as a new entry arrives on the stream
  # The method is passed as parameter when creating the instance for the TwitterStreamAPI
  # TwitterStreamAPI(access_key, access_secret, self.on_receive_new_entry_into_stream_callback)
  def on_receive_new_entry_into_stream_callback(self, entry):
    if 'user' in entry:                                                                     # tweet
      if not entry['user']['screen_name'] == self.user.screen_name:                         # not sent by the own user
        logp("Inserting new tweet on the stream Queue: %s" % entry)
        self.tweet_stream.put(entry)                                                        # put the new tweet on the stream queue
        self.refresh_emblem_counter()
        if self.config['alert']:                                                            # user wants alert?
          self.send_alert()
    elif 'direct_message' in entry:                                                         # direct messages
      if not entry['direct_message']['sender']['screen_name'] == self.user.screen_name:     # not sent by the own user
        logp("Inserting new message on the message Queue: %s" % entry)
        self.message_stream.put(entry)                                                      # put the new message on the message queue
        self.refresh_emblem_counter()
        if self.config['alert']:                                                            # user wants alert?
          self.send_alert()

  def show_user_timeline(self):
    self.inform_start_of_waiting_process()
    
    timeline = self.api.user_timeline()
    if len(timeline) > 0:
      ut_menu = menu.Menu(self.icon)                                                        # callback not set since there is no action when clicking ...
      for status in timeline:                                                               # ... on the menu generated from this list
        text = status['text']
        sender = status['user']['name']
        uid = status['id_str']
        ut_menu.add(Tweet(text, sender, uid))
      ut_menu.pop_up()
    else:
      message = _("Oh, dear, your timeline is empty :-(")
      dialog = {'use-markup':True}
      self.show_popup_message(message, dialog)

    self.inform_end_of_waiting_process()

  def show_new_tweets(self):
    self.inform_start_of_waiting_process()
    
    nt_menu = menu.Menu(self.icon, self.on_tweet_list_menu_clicked)
    while not self.tweet_stream.empty():                                                    # iterate on the stream composing the message
      tweet = self.tweet_stream.get()
      text = tweet['text']
      sender = tweet['user']['name']
      uid = tweet['id_str']
      nt_menu.add(Tweet(text, sender, uid))
    nt_menu.pop_up()
    
    self.refresh_emblem_counter() 
    self.inform_end_of_waiting_process()

  def show_home_timeline(self):
    self.inform_start_of_waiting_process()
    
    timeline = self.api.home_timeline()
    if len(timeline) > 0:
      ht_menu = menu.Menu(self.icon, self.on_tweet_list_menu_clicked)
      for status in timeline:
        text = status['text']
        sender = status['user']['screen_name']
        uid = status['id_str']
        ht_menu.add(Tweet(text, sender, uid))
      ht_menu.pop_up()
    else:
      message = _("Oh, dear, your timeline is empty :-(")
      dialog = {'use-markup':True}
      self.show_popup_message(message, dialog)
      
    self.inform_end_of_waiting_process()

  def show_new_direct_messages(self):
    self.inform_start_of_waiting_process()
    
    dm_menu = menu.Menu(self.icon, self.on_direct_messages_list_menu_clicked)
    while not self.message_stream.empty():                                                  # iterate on the stream composing the message
      direct_message = self.message_stream.get()
      text = direct_message['direct_message']['text']
      sender = direct_message['direct_message']['sender']['screen_name']
      dm_menu.add(DirectMessage(text, sender))
    dm_menu.pop_up()

    self.refresh_emblem_counter()
    self.inform_end_of_waiting_process()
    
  def show_direct_messages(self):
    self.inform_start_of_waiting_process()

    messages = self.api.direct_messages()
    if len(messages) > 0:
      dm_menu = menu.Menu(self.icon, self.on_direct_messages_list_menu_clicked)
      for status in messages:
        text = status['text']
        sender = status['sender']['screen_name']
        dm_menu.add(DirectMessage(text, sender))
      dm_menu.pop_up()
    else:
      dialog = {'use-markup':True}
      message = _("Oh, dear, you do not have direct messages :-(")
      self.show_popup_message(message, dialog)

    self.inform_end_of_waiting_process()
    
  def on_direct_messages_list_menu_clicked(self, widget):
    self.ask_for_direct_message_reply(widget.get_label())                           # label contains the sender of the message, reply to him/her now
    
  def on_tweet_list_menu_clicked(self, widget):
    self.ask_for_retweet(widget.get_label())                                        # label contains the id of the tweet needed to retweet it

  def tweet(self, message):                                                         # popularly "send a tweet"
    self.inform_start_of_waiting_process()
    self.api.update_status(message)
    self.inform_end_of_waiting_process()

  def show_credentials(self):
    self.inform_start_of_waiting_process()
    credentials = self.api.verify_credentials()
    message = _("%s [<b>%s</b>]\nFollowers: %s\nFriends: %s\nTweets: %s\n") % (credentials['name'], credentials['screen_name'], credentials['followers_count'], credentials['friends_count'], credentials['statuses_count'])
    dialog = {'use-markup':True}
    self.inform_end_of_waiting_process()
    self.show_popup_message(message, dialog)

  # Applet methods

  def ask_for_direct_message_reply(self, destinatary):
    dialog = {'buttons':'ok;cancel'}
    widget = {'widget-type':'text-entry', 'nb-chars':self.tweet_length + 1}         # 140 characters max, a direct message
    self.show_popup_message((_("%s, write a reply to %s")) % (self.user.screen_name, destinatary), dialog, widget)
    self.dialog_type = self.responding_sending_direct_message_reply
    self.replying_direct_message_to = destinatary

  # Opens the dialog to be filled with the tweet and also
  # informs about tweet longer than 140 characters
  def ask_for_tweet(self, default="", warning=False):
    dialog = {'buttons':'ok;cancel'}
    widget = {'widget-type':'text-entry', 'nb-chars':self.tweet_length + 1, 'initial-value':default}  # 140 characters max, a tweet :)
    if not warning:
      message = _("%s, send a tweet") % self.user.screen_name
      self.dialog_type = self.responding_tweet
    else:                                                                                             # user entered more than 140 characters
      message = _("Your tweet has more than %d characters, please make it shorter") % self.tweet_length
      self.dialog_type = self.responding_tweet
    self.show_popup_message(message, dialog, widget)
    
  def ask_for_retweet(self, tweet_id):
    dialog = {'buttons':'ok;cancel'}
    self.show_popup_message((_("%s, retweet?")) % self.user.screen_name, dialog)
    self.dialog_type = self.responding_retweet
    self.retweet_tweet_id = tweet_id

  # TODO: Implement it as a config file using screen_name as section index
  def read_user_data(self):
    """Read the users file formated as Screen Name<space>Access Key<space>Access Secret"""
    found = False
    if os.path.exists(self.user_file):
      if os.path.getsize(self.user_file) > 0:
        f = open(self.user_file, "rb")
        data = f.read()
        self.user.screen_name, self.user.access_key, self.user.access_secret = data.split()   # split the line by space token
        f.close()
        found = True
    return found

  def write_user_data(self):
    f = open(self.user_file, 'w')
    f.write("%s %s %s" % (self.user.screen_name, self.user.access_key, self.user.access_secret))
    f.close()

  def show_initial_informations(self):
    message = _("Twitter Applet needs your nickname, and an authorization\nthat you accept it to connect on your Twitter account")
    dialog = {'buttons':'next'}
    self.show_popup_message(message, dialog)
    self.dialog_type = self.responding_initial_informations

  def ask_for_screen_name(self):
    message = _("What is your Twitter nickname?")
    dialog = {'buttons':'next'}
    widget = {'widget-type':'text-entry'}
    self.show_popup_message(message, dialog, widget)
    self.dialog_type = self.responding_screen_name

  def ask_for_authorization(self):
    authorization_url = self.twitter_auth.get_authorization_url()
    logp("Opening the auth URL '%s'" % authorization_url)
    dialog = {'buttons':'next'}
    try:
      webbrowser.open(authorization_url)
      message = _("Twitter applet needs you to give the authorization. Authorization page was opened on your browser. As soon as you authorize it, copy the PIN number that will be shown, and close this dialog")
      self.show_popup_message(message, dialog)
    except webbrowser.Error:    
      message = _("Twitter applet needs you to give the authorization. Copy the address bellow and access it with your browser. Copy the PIN number that will be shown as soon as you authorize")
      widget = {'widget-type':'text-entry', 'initial-value':authorization_url}
      self.show_popup_message(message, dialog, widget)
    self.dialog_type = self.responding_authorization

  def ask_for_pin_number(self):
    message = _("Enter the PIN number on the authorization page")
    dialog = {'buttons':'next'}
    widget = {'widget-type':'text-entry'}
    self.show_popup_message(message, dialog, widget)
    self.dialog_type = self.responding_pin

  def show_popup_successful_connection(self):
    self.show_popup_message(_("Successfully connected with Twitter"))

  def show_popup_message(self, message, dialog={}, widget={}):
    dialog_attributes = {'message':message}
    widget_attributes = {}
    dialog_attributes.update(dialog)
    widget_attributes.update(widget)
    self.icon.PopupDialog (dialog_attributes, widget_attributes)

  def build_direct_messages_menu(self):
    direct_messages_menu = []
    if self.message_stream.empty():
      label = _("Direct messages")
    else:
      label = _("New direct messages (%d)" % self.message_stream.qsize())
    direct_messages_menu.append ({
        'type'  : CDApplet.MENU_ENTRY,
        'label' : label,
        'id'    : self.direct_messages_menu_id,
        'icon'  : os.path.abspath("./data/message.png")
    })
    self.icon.AddMenuItems(direct_messages_menu)

  def build_credentials_menu(self):
    credentials_menu = []
    credentials_menu.append ({
        'type'  : CDApplet.MENU_ENTRY,
        'label' : _("Info"),
        'id'    : self.credentials_menu_id,
        'icon'  : os.path.abspath("./data/credentials.png")
    })
    self.icon.AddMenuItems(credentials_menu)
    
  def build_tweets_menu(self):
    tweets_menu = []
    if self.tweet_stream.empty():
      label = _("Tweets")
    else:
      label = _("New tweets (%d)" % self.tweet_stream.qsize())
    tweets_menu.append ({
        'type'  : CDApplet.MENU_ENTRY,
        'label' : label,
        'id'    : self.tweets_menu_id,
        'icon'  : os.path.abspath("./data/tweet.png")
    })
    self.icon.AddMenuItems(tweets_menu)
    
  def build_user_timeline_menu(self):
    user_timeline_menu = []
    
    user_timeline_menu.append ({
        'type'  : CDApplet.MENU_ENTRY,
        'label' : _("My tweets"),
        'id'    : self.user_timeline_menu_id,
        'icon'  : os.path.abspath("./data/tweet.png")
    })
    self.icon.AddMenuItems(user_timeline_menu)

  def __init__(self):
    self.user = User()
    self.user_file = os.path.abspath(os.path.join(os.getcwd(),'..','..','.twitter_users'))      # ~/.config/cairo-dock/.twitter_users
    self.twitter_auth = TwitterOauth()
    
    self.api = None
    self.stream_api = None
    
    (self.responding_screen_name, self.responding_authorization, self.responding_pin,
    self.responding_success, self.responding_tweet, self.responding_initial_informations,
    self.responding_sending_direct_message_reply, self.responding_retweet) = range(8)
    self.dialog_type = None
    
    self.emblem = emblem.Emblem()                                                                     # emblem maker, see emblem.py

    self.direct_messages_menu_id  = 1000
    self.credentials_menu_id      = 2000
    self.tweets_menu_id           = 3000
    self.user_timeline_menu_id    = 4000
    
    self.tweet_length             = 140
    
    self.tweet_stream   = Queue.Queue()
    self.message_stream = Queue.Queue()

    CDApplet.__init__(self)                                                                           # call CDApplet interface init

  # Inherited methods from CDApplet
  def begin(self):
    logp("Looking for user ...")
    if not self.read_user_data():                                                                     # first time for the user
      logm("User not found")
      self.show_initial_informations()                                                                # start the wizard
    else:                                                                                             # user not found
      logp("User '%s' found" % self.user.screen_name)
      self.api = TwitterAPI(self.user.access_key, self.user.access_secret)                            # getting control over the api
      # setting the callback to receive the data of every entry on the stream
      self.stream_api = TwitterStreamAPI(self.user.access_key, self.user.access_secret, self.on_receive_new_entry_into_stream_callback)

  def get_config(self, keyfile):
    self.config['emblem_size'] = keyfile.get('Configuration', 'emblem_size')
    self.config['alert'] = keyfile.getboolean('Configuration', 'alert')
    self.config['sound'] = keyfile.getboolean('Configuration', 'sound')
    self.config['animation'] = keyfile.getboolean('Configuration', 'animation')
    self.config['animation_type'] = keyfile.get('Configuration', 'animation_type')

  def reload(self):
    self.refresh_emblem_counter()
    self.refresh_emblem_size()

  # Callbacks
  def on_answer_dialog(self, key, content):
    if (key == 0 or key == -1):                                                                       # ... and pressed Ok or Enter
      if self.dialog_type == self.responding_initial_informations:
        self.ask_for_screen_name()
      elif self.dialog_type == self.responding_screen_name:                                           # user typed screen name ...
        logp("Receiving screen name '%s'" % content)
        self.user.screen_name = content
        self.ask_for_authorization()
      elif self.dialog_type == self.responding_authorization:
        logp("Asking for PIN")
        self.ask_for_pin_number()                                                     # ask for the PIN number received when acessed the auth URL
      elif self.dialog_type == self.responding_pin:                                   # user typed the PIN number
        logp("Receiving PIN: %s" % content)
        self.user.access_key, self.user.access_secret = self.twitter_auth.get_access_token_and_secret(content)
        logp("Writing user data")
        self.write_user_data()                                                        # writing the new users data
        self.api = TwitterAPI(self.user.access_key, self.user.access_secret)          # getting control over the api
        if self.api:
          self.show_popup_successful_connection()
        else:
          logm("A problem has occurred while getting access to the API")
      elif self.dialog_type == self.responding_tweet:
        if len(content) > self.tweet_length:
            self.ask_for_tweet(content, True)
        else:
          logp("Sending a tweet '%s'" % content)
          self.api.tweet(content)
      elif self.dialog_type == self.responding_sending_direct_message_reply:
        logp("Sending a direct message '%s'" % content)
        self.api.new_direct_message(content, self.replying_direct_message_to)
      elif self.dialog_type == self.responding_retweet:
        logp("Retweeting")
        self.api.retweet(self.retweet_tweet_id)

  # If there is nothing to be seen, the left click opens the dialog to write a tweet
  # Otherwise we check if there is something new, likely to be what the user wants
  # to see quickly
  def on_click(self, key):
    if not self.tweet_stream.empty():
      self.show_new_tweets()
    elif not self.message_stream.empty():
      self.show_new_direct_messages()
    else:
      self.ask_for_tweet()

  def on_middle_click(self):
    self.show_home_timeline()

  def on_build_menu(self):
    self.build_credentials_menu()
    self.build_user_timeline_menu()
    self.build_direct_messages_menu()
    self.build_tweets_menu()

  def on_menu_select(self, selected_menu):
    if selected_menu == self.direct_messages_menu_id:
      if self.message_stream.empty():                                                 # there are not new direct messages
        self.show_direct_messages()                                                   # show the last 20
      else:                                                                           # new direct messages on the stream
        self.show_new_direct_messages()                                               # show new direct messages
    elif selected_menu == self.credentials_menu_id:
      self.show_credentials()
    elif selected_menu == self.tweets_menu_id:
      if self.tweet_stream.empty():                                                   # there are not new tweets
        self.show_home_timeline()                                                     # show the last 20 tweets on the home timeline
      else:                                                                           # new tweets on the stream
        self.show_new_tweets()                                                        # show new tweets
    elif selected_menu == self.user_timeline_menu_id:
      self.show_user_timeline()

if __name__ == '__main__':
  Applet().run()
