Class: ChatWindow

Inherits:
Gtk::Window
  • Object
show all
Defined in:
lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb

Instance Method Summary collapse

Constructor Details

#initialize(jid, password, mucjid) ⇒ ChatWindow

Returns a new instance of ChatWindow.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb', line 143

def initialize(jid, password, mucjid)
  super(Gtk::Window::TOPLEVEL)
  self.title = "GtkMUCClient: #{mucjid.resource} in #{mucjid.strip}"
  signal_connect("delete_event") { destroy; Gtk.main_quit }

  layout = Gtk::VBox.new

  @topic = Gtk::Entry.new
  @topic.editable = false
  @topic.show
  layout.pack_start(@topic, false, false, 2)

  layout_mid = Gtk::HPaned.new
  layout_mid.position = 500
  layout_mid.show
  layout.pack_start(layout_mid)

  @buffer_scroll = Gtk::ScrolledWindow.new
  @buffer_scroll.show
  @buffer_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
  layout_mid.pack1(@buffer_scroll, true, true)

  @buffer_view = Gtk::TextView.new
  @buffer_view.editable = false
  @buffer_view.cursor_visible = false
  @buffer_view.wrap_mode = Gtk::TextTag::WRAP_WORD
  @buffer_view.modify_font(Pango::FontDescription.new('monospace 12'))
  @buffer_view.show
  @buffer_scroll.add_with_viewport(@buffer_view)

  roster_scroll = Gtk::ScrolledWindow.new
  roster_scroll.show
  roster_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
  layout_mid.pack2(roster_scroll, true, true)

  @roster = Gtk::ListStore.new(String)
  @roster.set_sort_column_id(0)
  roster_view = Gtk::TreeView.new(@roster)
  roster_view.append_column Gtk::TreeViewColumn.new("Participant", Gtk::CellRendererText.new, {:text => 0})
  roster_view.show
  roster_scroll.add_with_viewport(roster_view)

  @input = Gtk::Entry.new
  @input.grab_focus
  @input.signal_connect("activate") {
    on_input(@input.text)
    @input.text = ''
  }
  @input.show
  layout.pack_start(@input, false, false, 2)

  layout.show
  add(layout)

  print_buffer "Welcome to the XMPP4R sample GTK2 Multi-User Chat client"
  print_buffer "Commands start with a slash, type \"/help\" for a list"

  @client = Jabber::Client.new(jid)
  Jabber::Version::SimpleResponder.new(@client, "XMPP4R example: GtkMUCClient", Jabber::XMPP4R_VERSION, IO.popen("uname -sr").readlines.to_s.strip)
  Thread.new {
    begin
      print_buffer "Connecting for domain #{jid.domain}..."
      @client.connect
      print_buffer "Authenticating for #{jid.strip}..."
      @client.auth(password)
      print_buffer "Attempting to join #{mucjid.strip} as #{mucjid.resource}..."
      @muc = Jabber::MUC::SimpleMUCClient.new(@client)
      register_handlers
      @muc.join(mucjid)
    rescue Exception => e
      puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
      print_buffer("Error: #{e}")
    end
  }

  set_size_request(600, 400)
end

Instance Method Details

#on_input(line) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb', line 259

def on_input(line)
  commands = {
    'help' => [
      'Display this help',
      lambda {
        commands.each { |cmd,a|
          print_buffer "/#{cmd.ljust(10)} - #{a[0]}"
        }
      }
    ],
    'msg' => [
      'Send a private message to a user',
      lambda { |args|
        # Limitation: it is not possible to send private messages
        # to a user with a space in his nickname
        to = args.shift
        text = args.join(' ')
        @muc.say(text, to)
        print_buffer "->(#{to}) #{text}"
      }
    ],
    'subject' => [
      'Change the room\'s subject',
      lambda { |args|
        @muc.subject = args.join(' ')
      }
    ],
    'quit' => [
      'Leave room with optional message, then disconnect client and shut down',
      lambda { |args|
        @muc.exit(args.join(' ')) if @muc.active?
        @client.close
        Gtk.main_quit
      }
    ]
  }

  if line =~ /^\//
    args = line.split(/ /)
    cmd = args.shift[1..-1].downcase

    command = commands[cmd]
    if command
      help, func = command
      func.call(args)
    else
      print_buffer "Unknown command: #{cmd}, use \"/help\""
    end
  else
    @muc.say(line)
  end
end


221
222
223
224
225
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb', line 221

def print_buffer(s, time=nil)
  @buffer_view.buffer.insert(@buffer_view.buffer.end_iter, "[#{(time || Time.new).getlocal.strftime('%I:%M')}] #{s}\n")
  va = @buffer_scroll.vadjustment
  va.value = va.upper
end

#register_handlersObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb', line 227

def register_handlers
  @muc.on_room_message { |time,text|
    print_buffer("*** #{text}", time)
  }
  @muc.on_message { |time,nick,text|
    if text =~ /^\/me (.+)$/
      print_buffer("*#{nick} #{$1}", time)
    else
      print_buffer("<#{nick}> #{text}", time)
    end
  }
  @muc.on_private_message { |time,nick,text|
    print_buffer("<-(#{nick}) #{text}", time)
  }
  @muc.on_join { |time,nick|
    @roster.append[0] = nick
  }
  @muc.on_self_leave { |time|
    print_buffer("You have exited the room", time)
  }
  @muc.on_leave { |time,nick|
    del_iter = nil
    @roster.each { |m,p,iter|
      del_iter = iter if iter[0] == nick
    }
    @roster.remove(del_iter) if del_iter
  }
  @muc.on_subject { |time,nick,subject|
    @topic.text = subject
  }
end