Rename Dialog プラグインはなんとか完成した。
本サイトに eog-plugin 付きのを置いているけどコードも貼っておきます。
日本語が読めないのに海外からコードを探しに来る人もいるって解ったし。
コメントは日本語でいくけど。
#-*- coding:utf-8 -*-
# Eye of GNOME renamedlg plugin version 1.0.0
# Copyright © 2009 sasakima-nao <m6579ne998z@gmail.com>
#
# 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.
# Eye of GNOME Documentation
# http://library.gnome.org/devel/eog/stable/index.html
import eog
import gtk
import os
import gio
ui_str = """<ui>
<menubar name="MainMenu">
<menu action="Edit">
<separator/>
<menuitem action="rename"/>
</menu>
</menubar>
</ui>"""
class RenameDlgPlugin(eog.Plugin):
"""
ダイアログを出しリネームを行うプラグイン
"""
def __init__(self):
eog.Plugin.__init__(self)
def activate(self, window):
"""
window ポインタをどこかに保存するとプロセスが終了しなくなる
GtkActionGroup.add_actions でシグナルの user_data でも駄目
ということで F2 キーとメニューのシグナル処理は別々に作る
"""
uimanager = window.get_ui_manager()
# F2 キーでの処理
accelgroup = uimanager.get_accel_group()
accelgroup.connect_group(gtk.keysyms.F2, 0, gtk.ACCEL_VISIBLE, self.on_acc)
# メニューを作るだけの処理
action_group = gtk.ActionGroup("RenameActions")
actions = [("rename", None, "リネーム(_R)", None, "リネーム", None)]
action_group.add_actions(actions)
uimanager.insert_action_group(action_group, 0)
self._ui_id = uimanager.add_ui_from_string(ui_str)
# 普通にコネクトなら window を user_dataにしても大丈夫
m = uimanager.get_widget("/MainMenu/Edit/rename")
m.connect("activate", self.on_rename, window)
def update_ui(self, window):
pass
def deactivate(self, window):
uimanager = window.get_ui_manager()
uimanager.remove_ui(self._ui_id)
accelgroup = uimanager.get_accel_group()
accelgroup.disconnect_key(gtk.keysyms.F2, 0)
uimanager.ensure_update()
def on_acc(self, accelGroup, window, keyval, modifier):
self.on_rename(None, window)
def on_rename(self, widget, window):
#print dir(window)
if window == None:
return
img = window.get_image()
if img == None:
return
fullname = img.get_uri_for_display()[7:]
path, name = os.path.split(fullname)
label = gtk.Label(name)
entry = gtk.Entry()
entry.set_text(name)
d = gtk.Dialog( "リネーム",
window,
gtk.DIALOG_MODAL,
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT) )
try:
d.vbox.pack_start(label, False)
d.vbox.pack_start(entry, False)
d.show_all()
def dlg_ok(self):
d.response(gtk.RESPONSE_ACCEPT)
entry.connect("activate", dlg_ok)
# 成功かキャンセルまでループ
while 1:
if d.run() == gtk.RESPONSE_ACCEPT:
text = entry.get_text()
if text == name:
self.messagebox("変更されていません", window)
else:
if text in os.listdir(path):
self.messagebox("同一ファイル名が見つかりました", window)
else:
# EogListStore から削除
store = window.get_store()
store.remove_image(img)
# リネームさせる
newname = os.path.join(path, text)
os.rename(fullname, newname)
# 一応キューを回しておく
while gtk.events_pending():
gtk.main_iteration()
# EogImage として登録
f = gio.File(newname)
newimg = eog.eog_image_new_file(f)
# EogListStore に突っ込む
store.append_image(newimg)
# EogThumbView に選択させれば EogScrollView に反映
tv = window.get_thumb_view()
tv.set_current_image(newimg, True)
break
else:
break
finally:
d.destroy()
def messagebox(self, text, window):
dlg = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING,
gtk.BUTTONS_OK, text)
dlg.set_title("Eye of GNOME")
r = dlg.run()
dlg.destroy()
こうやって貼り付けると「なんだ、簡単なんだ」思われそう。
せっかくなのでコードの整理をする前の一部分を貼り付けておきます。
store = window.get_store()
store.remove_image(img)
newname = os.path.join(path, text)
#
os.rename(fullname, newname)
while gtk.events_pending():
gtk.main_iteration()
f = gio.File(newname)
newimg = eog.eog_image_new_file(f)
# EogListStore に突っ込む
store.append_image(newimg)
i = store.get_pos_by_image(newimg)
tv = window.get_thumb_view()
tv.set_current_image(newimg, True)
#
#view = window.get_view()
#view.set_image(store.get_image_by_pos(i))
#
# GLocalFile を得てコピーを作る
"""oldimg = img.get_file()
f = gio.File(newname)
print dir(f)"""
#newimg = eog.eog_image_new_file(f)
"""oldimg.copy(f)
newimg = eog.eog_image_new_file(f)
# EogListStore に突っ込む
store = window.get_store()
#i = store.get_pos_by_image(newimg)
store.append_image(newimg)
while gtk.events_pending():
gtk.main_iteration()
view = window.get_view()
view.set_image(newimg)
store.remove_image(img)
oldimg.remove()"""
#print old
#print dir(old)
"""os.rename(fullname, newname)
while gtk.events_pending():
gtk.main_iteration()
f = gio.File(newname)
newimg = eog.eog_image_new_file(f)"""
#img.load(newimg, 0)#, (eog.IMAGE_DATA_IMAGE, eog.IMAGE_DATA_DIMENSION))#, None, None)
#view = window.get_view()
#view.set_image(newimg)
"""store = window.get_store()
store.append_image(newimg)
v = window.get_thumb_view()
v.set_current_image(i, True)"""
とにかく情報らしい情報が無いも同然だから自力で漁ったのよこの方法。
eog のソースに eog_window_display_image なんて関数があるが外部提供されていないし。
これ以上書くと愚痴になるのでおしまい。
しかしこれで Eye of GNOME への不満は私的には解消だ、他を探す必要もない。
プラグインを作るって面白いよ、アプリは探すものではなく作ったり改造したりするものだ。