spec_helper.rb 2.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#  Copyright (C) 2009 by Cies Breijs
#  Copyright (C) 2009 by Niels Slot
#
#  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 2 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.
#
#  You should have received a copy of the GNU General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA 02110-1301, USA.

Cies Breijs's avatar
 
Cies Breijs committed
19 20 21
require 'rubygems'
require 'singleton'

22 23 24 25 26 27 28 29 30 31 32
begin
  require 'Qt'
rescue LoadError
  begin
    require 'rbus'
  rescue LoadError
    puts "Couldn't load either rdbus or Qt, please install one, quiting now.."
    exit
  end
end

Cies Breijs's avatar
 
Cies Breijs committed
33 34 35 36
class Interpreter
  include Singleton

  def connect
37
    @pid = ENV['KTURTLE_INTERPRETER_DBUS_PID'] || IO.readlines("pid")[0].to_i
Cies Breijs's avatar
 
Cies Breijs committed
38
    puts "Opening DBus connection with KTurtle (pid: #{@pid})..."
39 40 41 42 43 44 45 46

    # Use Qt DBus if it's found, otherwise fall back to rbus
    if Object.const_defined?(:Qt)
      @interpreter = Qt::DBusInterface.new("org.kde.kturtle-#{@pid}", '/Interpreter', 'org.kde.kturtle.Interpreter')
    else
      @interpreter = RBus.session_bus.get_object("org.kde.kturtle-#{@pid}", '/Interpreter')
      @interpreter.interface!('org.kde.kturtle.Interpreter')
    end
Cies Breijs's avatar
 
Cies Breijs committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  end

  def load(code); connect unless @pid; @interpreter.initialize code; nil; end
  def interpret;  @interpreter.interpret; nil; end
  def errors?;    @interpreter.encounteredErrors; end
  def errors;     @interpreter.getErrorStrings; end
  def state;      [:uninitialized, :initialized, :parsing, :executing, :finished, :aborted][@interpreter.state]; end
  def inspect;    "#<Interpreter pid:#{@pid}>"; end

  def run(code)
    load code
    while not [:finished, :aborted].include? state
      interpret
    end
    self  # return self for easy method stacking
  end

  def should_run_clean(code)
    run(code)
    errors?.should == false
    p errors if errors?
  end
end