require 'Qt4'
load 'rtranslatorui.rb'
load 'resources.rb'

class TranslatorApp < Qt::Application
     def getToken
          service = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
          request = Qt::NetworkRequest.new(Qt::Url.new(service))
          request.setHeader(Qt::NetworkRequest::ContentTypeHeader, Qt::Variant.new('application/x-www-form-urlencoded'))
          params = Qt::Url.new
          params.addQueryItem('grant_type', 'client_credentials')
          params.addQueryItem('client_id', 'RTranslate')
          params.addQueryItem('client_secret', '0Gb3gKf/1LsZcq9lNCLSKW8B3sKFO6vCUiI/36GBcYM=')
          params.addQueryItem('scope', 'http://api.microsofttranslator.com')
          data = params.encodedQuery
          connect(@manager, SIGNAL('finished(QNetworkReply*)'), self, SLOT('finishedAuth(QNetworkReply*)'))
          @manager.post(request, data)
     end
     
     def finishedAuth(reply)
          puts reply.readAll
          
          disconnect(@manager)
     end
     
     def initialize
          super ARGV
          @manager = Qt::NetworkAccessManager.new
          @codes = Array.new
          @names = Array.new
          @token = String.new
          @ui = Ui_RTranslator.new
          @window = Qt::MainWindow.new
          @ui.setupUi(@window)
          getToken
          @window.show
     end
     slots 'finishedAuth(QNetworkReply*)'
end

a = TranslatorApp.new
a.exec
