fix tracker url
[blender-addons-contrib.git] / text_editor_pasteall.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "PasteAll",
21     "author": "Dalai Felinto (dfelinto)",
22     "version": (0,7),
23     "blender": (2, 6, 0),
24     "location": "Text editor > Properties panel",
25     "description": "Send your selection or text to www.pasteall.org",
26     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
27         "Scripts/Text_Editor/PasteAll",
28     "tracker_url": "https://projects.blender.org/tracker/index.php?"\
29         "func=detail&aid=23493",
30     "category": "Text Editor"}
31
32 # ########################################################
33 # PasteAll.org Text Sender Script
34 #
35 # Dalai Felinto (dfelinto)
36 # blenderecia.orgfree.com
37 #
38 # Rio de Janeiro - Brasil
39 # Vancouver - Canada
40 #
41 # Original code: 23rd August 2010 (Blender 2.5.3 rev. 31525)
42 #
43 # Important Note:
44 # This script is not official. I did it for fun and for my own usage.
45 # And please do not abuse of their generosity - use it wisely (a.k.a no flood).
46 #
47 # ########################################################
48
49
50 import bpy
51 import urllib
52 import urllib.request
53 import webbrowser
54
55 class TEXT_PT_pasteall(bpy.types.Panel):
56     bl_space_type = 'TEXT_EDITOR'
57     bl_region_type = 'UI'
58     bl_label = "PasteAll.org"
59
60     def draw(self, context):
61         layout = self.layout        
62         layout.operator("text.pasteall", icon='URL')
63         layout.prop(context.scene, "use_webbrowser")
64
65 class TEXT_OT_pasteall(bpy.types.Operator):
66     """"""
67     bl_idname = "text.pasteall"
68     bl_label = "PasteAll.org"
69     bl_description = "Send the current text or selection to www.pasteall.org"
70
71     @classmethod
72     def poll(cls, context):
73         if context.area.type != 'TEXT_EDITOR':
74             return False
75         else:
76             return context.space_data.text != None
77
78     def invoke(self, context, event):
79         import webbrowser
80         st = context.space_data
81
82         # get the selected text
83         text = self.get_selected_text(st.text)
84         # if no text is selected send the whole file
85         if text is None: text = st.text.as_string()
86
87         # get the file type based on the extension
88         format = self.get_file_format(st.text)
89
90         # send the text and receive the returned page
91         html = self.send_text(text, format)
92
93         if html is None:
94             self.report({'ERROR'}, "Error in sending the text to the server.")
95             return {'CANCELLED'}
96
97         # get the link of the posted page
98         page = self.get_page(str(html))
99         
100         if page is None or page == "":
101             self.report({'ERROR'}, "Error in retrieving the page.")
102             return {'CANCELLED'}
103         else:
104             self.report({'INFO'}, page)
105
106         # store the link in the clipboard
107         bpy.context.window_manager.clipboard = page
108
109         if context.scene.use_webbrowser:
110             try:
111                 webbrowser.open_new_tab(page)
112             except:
113                 self.report({'WARNING'}, "Error in opening the page %s." % (page))
114
115         return {'FINISHED'}
116             
117     def send_text(self, text, format):
118         """"""
119         import urllib
120         url = "http://www.pasteall.org/index.php"
121         values = {  'action' : 'savepaste',
122                     'parent_id' : '0',
123                     'language_id': format,
124                     'code' : text }
125
126         try:
127             data = urllib.parse.urlencode(values).encode()
128             req = urllib.request.Request(url, data)
129             response = urllib.request.urlopen(req)
130             page_source = response.read()
131         except:
132             return None
133         else:
134             return page_source
135
136     def get_page(self, html):
137         """"""
138         id = html.find('directlink')
139         id_begin = id + 12 # hardcoded: directlink">
140         id_end = html[id_begin:].find("</a>")
141
142         if id != -1 and id_end != -1:
143             return html[id_begin:id_begin + id_end]
144         else:
145             return None
146
147     def get_selected_text(self, text):
148         """"""
149         current_line = text.current_line
150         select_end_line = text.select_end_line
151         
152         current_character = text.current_character
153         select_end_character = text.select_end_character
154         
155         # if there is no selected text return None
156         if current_line == select_end_line:
157             if current_character == select_end_character:
158                 return None
159             else:
160                 return current_line.body[min(current_character,select_end_character):max(current_character,select_end_character)]
161
162         text_return = None
163         writing = False
164         normal_order = True # selection from top to bottom
165
166         for line in text.lines:
167             if not writing:
168                 if line == current_line:
169                     text_return = current_line.body[current_character:] + "\n"
170                     writing = True
171                     continue
172                 elif line == select_end_line:
173                     text_return =  select_end_line.body[select_end_character:] + "\n"
174                     writing = True
175                     normal_order = False
176                     continue
177             else:
178                 if normal_order:
179                     if line == select_end_line:
180                         text_return += select_end_line.body[:select_end_character]
181                         break
182                     else:
183                         text_return += line.body + "\n"
184                         continue
185                 else:
186                     if line == current_line:
187                         text_return += current_line.body[:current_character]
188                         break
189                     else:
190                         text_return += line.body + "\n"
191                         continue
192
193         return text_return
194     
195     def get_file_format(self, text):
196         """Try to guess what is the format based on the file extension"""
197         extensions =   {'diff':'24',
198                         'patch':'24',
199                         'py':'62',
200                         'c':'12',
201                         'cpp':'18'}
202
203         type = text.name.split(".")[-1]
204         return extensions.get(type, '0')
205
206 def register():
207     bpy.types.Scene.use_webbrowser = bpy.props.BoolProperty(
208         name='Launch Browser',
209         description='Opens the page with the submitted text.',
210         default=True)
211
212     bpy.utils.register_module(__name__)
213
214 def unregister():
215     del bpy.types.Scene.use_webbrowser
216     bpy.utils.unregister_module(__name__)
217
218 if __name__ == "__main__":
219     register()
220
221