You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

878 lines
34 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """Youtubedlg module responsible for the options window. """
  4. from __future__ import unicode_literals
  5. import os
  6. import gettext
  7. import wx
  8. from .version import __version__
  9. from .info import (
  10. __descriptionfull__,
  11. __licensefull__,
  12. __projecturl__,
  13. __appname__,
  14. __author__
  15. )
  16. from .utils import (
  17. TwoWayOrderedDict as twodict,
  18. os_path_exists,
  19. get_icon_file,
  20. read_formats
  21. )
  22. from .formats import (
  23. OUTPUT_FORMATS,
  24. VIDEO_FORMATS,
  25. AUDIO_FORMATS,
  26. FORMATS
  27. )
  28. #REFACTOR Move all formats, etc to formats.py
  29. class OptionsFrame(wx.Frame):
  30. """Youtubedlg options frame class.
  31. Args:
  32. parent (mainframe.MainFrame): Parent class.
  33. """
  34. FRAME_TITLE = _("Options")
  35. FRAMES_MIN_SIZE = (500, 440)
  36. def __init__(self, parent):
  37. wx.Frame.__init__(self, parent, title=self.FRAME_TITLE, size=parent.opt_manager.options["opts_win_size"])
  38. self.opt_manager = parent.opt_manager
  39. self.log_manager = parent.log_manager
  40. self.app_icon = None
  41. # Set the app icon
  42. app_icon_path = get_icon_file()
  43. if app_icon_path is not None:
  44. self.app_icon = wx.Icon(app_icon_path, wx.BITMAP_TYPE_PNG)
  45. self.SetIcon(self.app_icon)
  46. self._was_shown = False
  47. # Create options frame basic components
  48. self.panel = wx.Panel(self)
  49. self.notebook = wx.Notebook(self.panel)
  50. self.separator_line = wx.StaticLine(self.panel)
  51. self.reset_button = wx.Button(self.panel, label="Reset")
  52. self.close_button = wx.Button(self.panel, label="Close")
  53. # Create tabs
  54. tab_args = (self, self.notebook)
  55. self.tabs = (
  56. (GeneralTab(*tab_args), "General"),
  57. (FormatsTab(*tab_args), "Formats"),
  58. (DownloadsTab(*tab_args), "Downloads"),
  59. (AdvancedTab(*tab_args), "Advanced"),
  60. (ExtraTab(*tab_args), "Extra")
  61. )
  62. # Add tabs on notebook
  63. for tab, label in self.tabs:
  64. self.notebook.AddPage(tab, label)
  65. # Bind events
  66. self.Bind(wx.EVT_BUTTON, self._on_reset, self.reset_button)
  67. self.Bind(wx.EVT_BUTTON, self._on_close, self.close_button)
  68. self.Bind(wx.EVT_CLOSE, self._on_close)
  69. self.SetMinSize(self.FRAMES_MIN_SIZE)
  70. self._set_layout()
  71. self.load_all_options()
  72. def _set_layout(self):
  73. main_sizer = wx.BoxSizer(wx.VERTICAL)
  74. main_sizer.Add(self.notebook, 1, wx.EXPAND | wx.ALL, border=5)
  75. main_sizer.Add(self.separator_line, 0, wx.EXPAND)
  76. buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
  77. buttons_sizer.Add(self.reset_button)
  78. buttons_sizer.AddSpacer((5, -1))
  79. buttons_sizer.Add(self.close_button)
  80. main_sizer.Add(buttons_sizer, flag=wx.ALIGN_RIGHT | wx.ALL, border=5)
  81. self.panel.SetSizer(main_sizer)
  82. def _on_close(self, event):
  83. """Event handler for wx.EVT_CLOSE event."""
  84. self.save_all_options()
  85. self.GetParent()._update_videoformat_combobox()
  86. self.Hide()
  87. def _on_reset(self, event):
  88. """Event handler for the reset button wx.EVT_BUTTON event."""
  89. self.reset()
  90. def reset(self):
  91. """Reset the default options."""
  92. self.opt_manager.load_default()
  93. self.load_all_options()
  94. def load_all_options(self):
  95. """Load all the options on each tab."""
  96. for tab, _ in self.tabs:
  97. tab.load_options()
  98. def save_all_options(self):
  99. """Save all the options from all the tabs back to the OptionsManager."""
  100. for tab, _ in self.tabs:
  101. tab.save_options()
  102. def Show(self, *args, **kwargs):
  103. # CenterOnParent can't go to main frame's __init__ as main frame may change
  104. # own position and options frame won't be centered on main frame anymore.
  105. if not self._was_shown:
  106. self._was_shown = True
  107. self.CenterOnParent()
  108. return wx.Frame.Show(self, *args, **kwargs)
  109. class TabPanel(wx.Panel):
  110. """Main tab class from which each tab inherits.
  111. Args:
  112. parent (OptionsFrame): The parent of all tabs.
  113. notebook (wx.Notebook): The container for each tab.
  114. Notes:
  115. In order to use a different size you must overwrite the below *_SIZE
  116. attributes on the corresponding child object.
  117. """
  118. CHECKBOX_SIZE = (-1, -1)
  119. if os.name == "nt":
  120. # Make checkboxes look the same on Windows
  121. CHECKBOX_SIZE = (-1, 25)
  122. BUTTONS_SIZE = (-1, -1)
  123. TEXTCTRL_SIZE = (-1, -1)
  124. SPINCTRL_SIZE = (70, -1)
  125. CHECKLISTBOX_SIZE = (-1, 80)
  126. LISTBOX_SIZE = (-1, 80)
  127. def __init__(self, parent, notebook):
  128. wx.Panel.__init__(self, notebook)
  129. self.opt_manager = parent.opt_manager
  130. self.log_manager = parent.log_manager
  131. self.app_icon = parent.app_icon
  132. self.reset_handler = parent.reset
  133. # Shortcut methods below
  134. def crt_button(self, label, event_handler=None):
  135. button = wx.Button(self, label=label, size=self.BUTTONS_SIZE)
  136. if event_handler is not None:
  137. button.Bind(wx.EVT_BUTTON, event_handler)
  138. return button
  139. def crt_checkbox(self, label, event_handler=None):
  140. checkbox = wx.CheckBox(self, label=label, size=self.CHECKBOX_SIZE)
  141. if event_handler is not None:
  142. checkbox.Bind(wx.EVT_CHECKBOX, event_handler)
  143. return checkbox
  144. def crt_textctrl(self, style=None):
  145. if style is None:
  146. textctrl = wx.TextCtrl(self, size=self.TEXTCTRL_SIZE)
  147. else:
  148. textctrl = wx.TextCtrl(self, size=self.TEXTCTRL_SIZE, style=style)
  149. return textctrl
  150. def crt_combobox(self, choices, size=(-1, -1), event_handler=None):
  151. combobox = wx.ComboBox(self, choices=choices, size=size, style=wx.CB_READONLY)
  152. if event_handler is not None:
  153. combobox.Bind(wx.EVT_COMBOBOX, event_handler)
  154. return combobox
  155. def crt_spinctrl(self, spin_range=(0, 9999)):
  156. spinctrl = wx.SpinCtrl(self, size=self.SPINCTRL_SIZE)
  157. spinctrl.SetRange(*spin_range)
  158. return spinctrl
  159. def crt_statictext(self, label):
  160. return wx.StaticText(self, wx.ID_ANY, label)
  161. def crt_staticbox(self, label):
  162. return wx.StaticBox(self, wx.ID_ANY, label)
  163. def crt_checklistbox(self, choices, style=None):
  164. if style is None:
  165. checklistbox = wx.CheckListBox(self, choices=choices, size=self.CHECKLISTBOX_SIZE)
  166. else:
  167. checklistbox = wx.CheckListBox(self, choices=choices, style=style, size=self.CHECKLISTBOX_SIZE)
  168. return checklistbox
  169. def crt_listbox(self, choices, style=None):
  170. if style is None:
  171. listbox = wx.ListBox(self, choices=choices, size=self.LISTBOX_SIZE)
  172. else:
  173. listbox = wx.ListBox(self, choices=choices, style=style, size=self.LISTBOX_SIZE)
  174. return listbox
  175. class GeneralTab(TabPanel):
  176. LOCALE_NAMES = twodict([
  177. ('ar_AR', 'Arabic'),
  178. ('cs_CZ', 'Czech'),
  179. ('en_US', 'English'),
  180. ('fr_FR', 'French'),
  181. ('de_DE', 'German'),
  182. ('it_IT', 'Italian'),
  183. ('he_IS', 'Hebrew'),
  184. ('hu_HU', 'Hungarian'),
  185. ('pt_BR', 'Portuguese'),
  186. ('ru_RU', 'Russian'),
  187. ('es_ES', 'Spanish'),
  188. ('es_MX', 'Mexican Spanish'),
  189. ('tr_TR', 'Turkish')
  190. ])
  191. OUTPUT_TEMPLATES = [
  192. "Id",
  193. "Title",
  194. #"Url",
  195. "Ext",
  196. "Uploader",
  197. "Resolution",
  198. "Autonumber",
  199. "",
  200. #"License",
  201. #"Duration",
  202. "View Count",
  203. "Like Count",
  204. "Dislike Count",
  205. "Comment Count",
  206. "Average Rating",
  207. "Age Limit",
  208. "Width",
  209. "Height",
  210. #"Protocol",
  211. "Extractor",
  212. "",
  213. "Playlist",
  214. "Playlist Index",
  215. #"Playlist Id",
  216. #"Playlist Title"
  217. ]
  218. BUTTONS_SIZE = (30, -1)
  219. def __init__(self, *args, **kwargs):
  220. super(GeneralTab, self).__init__(*args, **kwargs)
  221. self.language_label = self.crt_statictext("Language")
  222. self.language_combobox = self.crt_combobox(list(self.LOCALE_NAMES.values()), event_handler=self._on_language)
  223. self.filename_format_label = self.crt_statictext("Filename format")
  224. self.filename_format_combobox = self.crt_combobox(list(OUTPUT_FORMATS.values()), event_handler=self._on_filename)
  225. self.filename_custom_format = self.crt_textctrl()
  226. self.filename_custom_format_button = self.crt_button("...", self._on_format)
  227. self.filename_opts_label = self.crt_statictext("Filename options")
  228. self.filename_ascii_checkbox = self.crt_checkbox("Restrict filenames to ASCII")
  229. self.more_opts_label = self.crt_statictext("More options")
  230. self.confirm_exit_checkbox = self.crt_checkbox("Confirm on exit")
  231. self.shutdown_checkbox = self.crt_checkbox("Shutdown on download completion", event_handler=self._on_shutdown)
  232. self.sudo_textctrl = self.crt_textctrl(wx.TE_PASSWORD)
  233. # Build the menu for the custom format button
  234. self.custom_format_menu = self._build_custom_format_menu()
  235. self._set_layout()
  236. self.confirm_exit_checkbox.Disable()
  237. if os.name == "nt":
  238. self.sudo_textctrl.Hide()
  239. self.sudo_textctrl.SetToolTip(wx.ToolTip("SUDO password"))
  240. def _set_layout(self):
  241. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  242. vertical_sizer = wx.BoxSizer(wx.VERTICAL)
  243. vertical_sizer.Add(self.language_label)
  244. vertical_sizer.Add(self.language_combobox, flag=wx.EXPAND | wx.ALL, border=5)
  245. vertical_sizer.Add(self.filename_format_label, flag=wx.TOP, border=5)
  246. vertical_sizer.Add(self.filename_format_combobox, flag=wx.EXPAND | wx.ALL, border=5)
  247. custom_format_sizer = wx.BoxSizer(wx.HORIZONTAL)
  248. custom_format_sizer.Add(self.filename_custom_format, 1, wx.ALIGN_CENTER_VERTICAL)
  249. custom_format_sizer.AddSpacer((5, -1))
  250. custom_format_sizer.Add(self.filename_custom_format_button)
  251. vertical_sizer.Add(custom_format_sizer, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  252. vertical_sizer.Add(self.filename_opts_label, flag=wx.TOP, border=5)
  253. vertical_sizer.Add(self.filename_ascii_checkbox, flag=wx.ALL, border=5)
  254. vertical_sizer.Add(self.more_opts_label, flag=wx.TOP, border=5)
  255. vertical_sizer.Add(self.confirm_exit_checkbox, flag=wx.ALL, border=5)
  256. shutdown_sizer = wx.BoxSizer(wx.HORIZONTAL)
  257. shutdown_sizer.Add(self.shutdown_checkbox, 2)
  258. shutdown_sizer.Add(self.sudo_textctrl, 1)
  259. vertical_sizer.Add(shutdown_sizer, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  260. main_sizer.Add(vertical_sizer, 1, wx.EXPAND | wx.ALL, border=5)
  261. self.SetSizer(main_sizer)
  262. def _build_custom_format_menu(self):
  263. menu = wx.Menu()
  264. for template in self.OUTPUT_TEMPLATES:
  265. if template:
  266. menu_item = menu.Append(wx.ID_ANY, template)
  267. menu.Bind(wx.EVT_MENU, self._on_template, menu_item)
  268. else:
  269. menu.AppendSeparator()
  270. return menu
  271. def _on_template(self, event):
  272. """Event handler for the wx.EVT_MENU of the custom_format_menu menu items."""
  273. label = self.custom_format_menu.GetLabelText(event.GetId())
  274. template = "-%({0})s".format(label.lower().replace(' ', '_'))
  275. custom_format = self.filename_custom_format.GetValue()
  276. custom_format += template
  277. self.filename_custom_format.SetValue(custom_format)
  278. def _on_format(self, event):
  279. """Event handler for the wx.EVT_BUTTON of the filename_custom_format_button."""
  280. event_object_pos = event.EventObject.GetPosition()
  281. event_object_height = event.EventObject.GetSize()[1]
  282. event_object_pos = (event_object_pos[0], event_object_pos[1] + event_object_height)
  283. self.PopupMenu(self.custom_format_menu, event_object_pos)
  284. def _on_language(self, event):
  285. """Event handler for the wx.EVT_COMBOBOX of the language_combobox."""
  286. wx.MessageBox("In order for the changes to take effect please restart {0}.".format(__appname__),
  287. "Restart",
  288. wx.OK | wx.ICON_INFORMATION,
  289. self)
  290. def _on_filename(self, event):
  291. """Event handler for the wx.EVT_COMBOBOX of the filename_format_combobox."""
  292. custom_selected = self.filename_format_combobox.GetValue() == OUTPUT_FORMATS[3]
  293. self.filename_custom_format.Enable(custom_selected)
  294. self.filename_custom_format_button.Enable(custom_selected)
  295. def _on_shutdown(self, event):
  296. """Event handler for the wx.EVT_CHECKBOX of the shutdown_checkbox."""
  297. self.sudo_textctrl.Enable(self.shutdown_checkbox.GetValue())
  298. #TODO Implement load-save for confirm_exit_checkbox widget
  299. def load_options(self):
  300. self.language_combobox.SetValue(self.LOCALE_NAMES[self.opt_manager.options["locale_name"]])
  301. self.filename_format_combobox.SetValue(OUTPUT_FORMATS[self.opt_manager.options["output_format"]])
  302. self.filename_custom_format.SetValue(self.opt_manager.options["output_template"])
  303. self.filename_ascii_checkbox.SetValue(self.opt_manager.options["restrict_filenames"])
  304. self.shutdown_checkbox.SetValue(self.opt_manager.options["shutdown"])
  305. self.sudo_textctrl.SetValue(self.opt_manager.options["sudo_password"])
  306. self._on_filename(None)
  307. self._on_shutdown(None)
  308. def save_options(self):
  309. self.opt_manager.options["locale_name"] = self.LOCALE_NAMES[self.language_combobox.GetValue()]
  310. self.opt_manager.options["output_format"] = OUTPUT_FORMATS[self.filename_format_combobox.GetValue()]
  311. self.opt_manager.options["output_template"] = self.filename_custom_format.GetValue()
  312. self.opt_manager.options["restrict_filenames"] = self.filename_ascii_checkbox.GetValue()
  313. self.opt_manager.options["shutdown"] = self.shutdown_checkbox.GetValue()
  314. self.opt_manager.options["sudo_password"] = self.sudo_textctrl.GetValue()
  315. class FormatsTab(TabPanel):
  316. AUDIO_QUALITY = twodict([("0", _("high")), ("5", _("mid")), ("9", _("low"))])
  317. def __init__(self, *args, **kwargs):
  318. super(FormatsTab, self).__init__(*args, **kwargs)
  319. self.video_formats_label = self.crt_statictext("Video formats")
  320. self.video_formats_checklistbox = self.crt_checklistbox(list(VIDEO_FORMATS.values()))
  321. self.audio_formats_label = self.crt_statictext("Audio formats")
  322. self.audio_formats_checklistbox = self.crt_checklistbox(list(AUDIO_FORMATS.values()))
  323. self.post_proc_opts_label = self.crt_statictext("Post-Process options")
  324. self.keep_video_checkbox = self.crt_checkbox("Keep original files")
  325. self.extract_audio_checkbox = self.crt_checkbox("Extract audio from video file")
  326. self.audio_quality_label = self.crt_statictext("Audio quality")
  327. self.audio_quality_combobox = self.crt_combobox(list(self.AUDIO_QUALITY.values()))
  328. self._set_layout()
  329. def _set_layout(self):
  330. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  331. vertical_sizer = wx.BoxSizer(wx.VERTICAL)
  332. vertical_sizer.Add(self.video_formats_label)
  333. vertical_sizer.Add(self.video_formats_checklistbox, 1, wx.EXPAND | wx.ALL, border=5)
  334. vertical_sizer.Add(self.audio_formats_label, flag=wx.TOP, border=5)
  335. vertical_sizer.Add(self.audio_formats_checklistbox, 1, wx.EXPAND | wx.ALL, border=5)
  336. vertical_sizer.Add(self.post_proc_opts_label, flag=wx.TOP, border=5)
  337. vertical_sizer.Add(self.keep_video_checkbox, flag=wx.ALL, border=5)
  338. vertical_sizer.Add(self.extract_audio_checkbox, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  339. audio_quality_sizer = wx.BoxSizer(wx.HORIZONTAL)
  340. audio_quality_sizer.Add(self.audio_quality_label, flag=wx.ALIGN_CENTER_VERTICAL)
  341. audio_quality_sizer.AddSpacer((20, -1))
  342. audio_quality_sizer.Add(self.audio_quality_combobox)
  343. vertical_sizer.Add(audio_quality_sizer, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  344. main_sizer.Add(vertical_sizer, 1, wx.EXPAND | wx.ALL, border=5)
  345. self.SetSizer(main_sizer)
  346. def load_options(self):
  347. checked_video_formats = [VIDEO_FORMATS[vformat] for vformat in self.opt_manager.options["selected_video_formats"]]
  348. self.video_formats_checklistbox.SetCheckedStrings(checked_video_formats)
  349. checked_audio_formats = [AUDIO_FORMATS[aformat] for aformat in self.opt_manager.options["selected_audio_formats"]]
  350. self.audio_formats_checklistbox.SetCheckedStrings(checked_audio_formats)
  351. self.keep_video_checkbox.SetValue(self.opt_manager.options["keep_video"])
  352. self.audio_quality_combobox.SetValue(self.AUDIO_QUALITY[self.opt_manager.options["audio_quality"]])
  353. self.extract_audio_checkbox.SetValue(self.opt_manager.options["to_audio"])
  354. def save_options(self):
  355. checked_video_formats = [VIDEO_FORMATS[vformat] for vformat in self.video_formats_checklistbox.GetCheckedStrings()]
  356. self.opt_manager.options["selected_video_formats"] = checked_video_formats
  357. checked_audio_formats = [AUDIO_FORMATS[aformat] for aformat in self.audio_formats_checklistbox.GetCheckedStrings()]
  358. self.opt_manager.options["selected_audio_formats"] = checked_audio_formats
  359. self.opt_manager.options["keep_video"] = self.keep_video_checkbox.GetValue()
  360. self.opt_manager.options["audio_quality"] = self.AUDIO_QUALITY[self.audio_quality_combobox.GetValue()]
  361. self.opt_manager.options["to_audio"] = self.extract_audio_checkbox.GetValue()
  362. class DownloadsTab(TabPanel):
  363. SUBS_LANG = twodict([
  364. ("en", _("English")),
  365. ("gr", _("Greek")),
  366. ("pt", _("Portuguese")),
  367. ("fr", _("French")),
  368. ("it", _("Italian")),
  369. ("ru", _("Russian")),
  370. ("es", _("Spanish")),
  371. ("tr", _("Turkish")),
  372. ("de", _("German"))
  373. ])
  374. FILESIZES = twodict([
  375. ("", "Bytes"),
  376. ("k", "Kilobytes"),
  377. ("m", "Megabytes"),
  378. ("g", "Gigabytes"),
  379. ("t", "Terabytes"),
  380. ("p", "Petabytes"),
  381. ("e", "Exabytes"),
  382. ("z", "Zettabytes"),
  383. ("y", "Yottabytes")
  384. ])
  385. SUBS_CHOICES = [
  386. "None",
  387. "Automatic subtitles (YOUTUBE ONLY)",
  388. "All available subtitles",
  389. "Subtitles by language"
  390. ]
  391. def __init__(self, *args, **kwargs):
  392. super(DownloadsTab, self).__init__(*args, **kwargs)
  393. self.subtitles_label = self.crt_statictext("Subtitles")
  394. self.subtitles_combobox = self.crt_combobox(self.SUBS_CHOICES, event_handler=self._on_subtitles)
  395. self.subtitles_lang_listbox = self.crt_listbox(list(self.SUBS_LANG.values()))
  396. self.subtitles_opts_label = self.crt_statictext("Subtitles options")
  397. self.embed_subs_checkbox = self.crt_checkbox("Embed subtitles into video file (mp4 ONLY)")
  398. self.playlist_box = self.crt_staticbox("Playlist")
  399. self.playlist_start_label = self.crt_statictext("Start")
  400. self.playlist_start_spinctrl = self.crt_spinctrl((1, 9999))
  401. self.playlist_stop_label = self.crt_statictext("Stop")
  402. self.playlist_stop_spinctrl = self.crt_spinctrl()
  403. self.playlist_max_label = self.crt_statictext("Max")
  404. self.playlist_max_spinctrl = self.crt_spinctrl()
  405. self.filesize_box = self.crt_staticbox("Filesize")
  406. self.filesize_min_label = self.crt_statictext("Min")
  407. self.filesize_min_spinctrl = self.crt_spinctrl((0, 1024))
  408. self.filesize_min_sizeunit_combobox = self.crt_combobox(list(self.FILESIZES.values()))
  409. self.filesize_max_label = self.crt_statictext("Max")
  410. self.filesize_max_spinctrl = self.crt_spinctrl((0, 1024))
  411. self.filesize_max_sizeunit_combobox = self.crt_combobox(list(self.FILESIZES.values()))
  412. self._set_layout()
  413. def _set_layout(self):
  414. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  415. vertical_sizer = wx.BoxSizer(wx.VERTICAL)
  416. vertical_sizer.Add(self.subtitles_label)
  417. vertical_sizer.Add(self.subtitles_combobox, flag=wx.EXPAND | wx.ALL, border=5)
  418. vertical_sizer.Add(self.subtitles_lang_listbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  419. vertical_sizer.Add(self.subtitles_opts_label, flag=wx.TOP, border=5)
  420. vertical_sizer.Add(self.embed_subs_checkbox, flag=wx.ALL, border=5)
  421. plist_and_fsize_sizer = wx.BoxSizer(wx.HORIZONTAL)
  422. plist_and_fsize_sizer.Add(self._build_playlist_sizer(), 1, wx.EXPAND)
  423. plist_and_fsize_sizer.AddSpacer((5, -1))
  424. plist_and_fsize_sizer.Add(self._build_filesize_sizer(), 1, wx.EXPAND)
  425. vertical_sizer.Add(plist_and_fsize_sizer, 1, wx.EXPAND | wx.TOP, border=5)
  426. main_sizer.Add(vertical_sizer, 1, wx.EXPAND | wx.ALL, border=5)
  427. self.SetSizer(main_sizer)
  428. def _build_playlist_sizer(self):
  429. playlist_box_sizer = wx.StaticBoxSizer(self.playlist_box, wx.VERTICAL)
  430. playlist_box_sizer.AddSpacer((-1, 10))
  431. border = wx.GridBagSizer(5, 40)
  432. border.Add(self.playlist_start_label, (0, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  433. border.Add(self.playlist_start_spinctrl, (0, 1))
  434. border.Add(self.playlist_stop_label, (1, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  435. border.Add(self.playlist_stop_spinctrl, (1, 1))
  436. border.Add(self.playlist_max_label, (2, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  437. border.Add(self.playlist_max_spinctrl, (2, 1))
  438. playlist_box_sizer.Add(border, flag=wx.ALIGN_CENTER)
  439. return playlist_box_sizer
  440. def _build_filesize_sizer(self):
  441. filesize_box_sizer = wx.StaticBoxSizer(self.filesize_box, wx.VERTICAL)
  442. border = wx.GridBagSizer(5, 20)
  443. border.Add(self.filesize_max_label, (0, 0), (1, 2), wx.ALIGN_CENTER_HORIZONTAL)
  444. border.Add(self.filesize_max_spinctrl, (1, 0))
  445. border.Add(self.filesize_max_sizeunit_combobox, (1, 1))
  446. border.Add(self.filesize_min_label, (2, 0), (1, 2), wx.ALIGN_CENTER_HORIZONTAL)
  447. border.Add(self.filesize_min_spinctrl, (3, 0))
  448. border.Add(self.filesize_min_sizeunit_combobox, (3, 1))
  449. filesize_box_sizer.Add(border, flag=wx.ALIGN_CENTER)
  450. return filesize_box_sizer
  451. def _on_subtitles(self, event):
  452. """Event handler for the wx.EVT_COMBOBOX of the subtitles_combobox."""
  453. self.subtitles_lang_listbox.Enable(self.subtitles_combobox.GetValue() == self.SUBS_CHOICES[-1])
  454. def load_options(self):
  455. if self.opt_manager.options["write_subs"]:
  456. self.subtitles_combobox.SetValue(self.SUBS_CHOICES[3])
  457. elif self.opt_manager.options["write_all_subs"]:
  458. self.subtitles_combobox.SetValue(self.SUBS_CHOICES[2])
  459. elif self.opt_manager.options["write_auto_subs"]:
  460. self.subtitles_combobox.SetValue(self.SUBS_CHOICES[1])
  461. else:
  462. self.subtitles_combobox.SetValue(self.SUBS_CHOICES[0])
  463. self.subtitles_lang_listbox.SetStringSelection(self.SUBS_LANG[self.opt_manager.options["subs_lang"]])
  464. self.embed_subs_checkbox.SetValue(self.opt_manager.options["embed_subs"])
  465. self.playlist_start_spinctrl.SetValue(self.opt_manager.options["playlist_start"])
  466. self.playlist_stop_spinctrl.SetValue(self.opt_manager.options["playlist_end"])
  467. self.playlist_max_spinctrl.SetValue(self.opt_manager.options["max_downloads"])
  468. self.filesize_min_spinctrl.SetValue(self.opt_manager.options["min_filesize"])
  469. self.filesize_max_spinctrl.SetValue(self.opt_manager.options["max_filesize"])
  470. self.filesize_min_sizeunit_combobox.SetValue(self.FILESIZES[self.opt_manager.options["min_filesize_unit"]])
  471. self.filesize_max_sizeunit_combobox.SetValue(self.FILESIZES[self.opt_manager.options["max_filesize_unit"]])
  472. self._on_subtitles(None)
  473. def save_options(self):
  474. subs_choice = self.SUBS_CHOICES.index(self.subtitles_combobox.GetValue())
  475. if subs_choice == 1:
  476. self.opt_manager.options["write_subs"] = False
  477. self.opt_manager.options["write_all_subs"] = False
  478. self.opt_manager.options["write_auto_subs"] = True
  479. elif subs_choice == 2:
  480. self.opt_manager.options["write_subs"] = False
  481. self.opt_manager.options["write_all_subs"] = True
  482. self.opt_manager.options["write_auto_subs"] = False
  483. elif subs_choice == 3:
  484. self.opt_manager.options["write_subs"] = True
  485. self.opt_manager.options["write_all_subs"] = False
  486. self.opt_manager.options["write_auto_subs"] = False
  487. else:
  488. self.opt_manager.options["write_subs"] = False
  489. self.opt_manager.options["write_all_subs"] = False
  490. self.opt_manager.options["write_auto_subs"] = False
  491. self.opt_manager.options["subs_lang"] = self.SUBS_LANG[self.subtitles_lang_listbox.GetStringSelection()]
  492. self.opt_manager.options["embed_subs"] = self.embed_subs_checkbox.GetValue()
  493. self.opt_manager.options["playlist_start"] = self.playlist_start_spinctrl.GetValue()
  494. self.opt_manager.options["playlist_end"] = self.playlist_stop_spinctrl.GetValue()
  495. self.opt_manager.options["max_downloads"] = self.playlist_max_spinctrl.GetValue()
  496. self.opt_manager.options["min_filesize"] = self.filesize_min_spinctrl.GetValue()
  497. self.opt_manager.options["max_filesize"] = self.filesize_max_spinctrl.GetValue()
  498. self.opt_manager.options["min_filesize_unit"] = self.FILESIZES[self.filesize_min_sizeunit_combobox.GetValue()]
  499. self.opt_manager.options["max_filesize_unit"] = self.FILESIZES[self.filesize_max_sizeunit_combobox.GetValue()]
  500. class AdvancedTab(TabPanel):
  501. TEXTCTRL_SIZE = (300, -1)
  502. def __init__(self, *args, **kwargs):
  503. super(AdvancedTab, self).__init__(*args, **kwargs)
  504. self.retries_label = self.crt_statictext("Retries")
  505. self.retries_spinctrl = self.crt_spinctrl((1, 999))
  506. self.auth_label = self.crt_statictext("Authentication")
  507. self.username_label = self.crt_statictext("Username")
  508. self.username_textctrl = self.crt_textctrl()
  509. self.password_label = self.crt_statictext("Password")
  510. self.password_textctrl = self.crt_textctrl(wx.TE_PASSWORD)
  511. self.video_pass_label = self.crt_statictext("Video password")
  512. self.video_pass_textctrl = self.crt_textctrl(wx.TE_PASSWORD)
  513. self.network_label = self.crt_statictext("Network")
  514. self.proxy_label = self.crt_statictext("Proxy")
  515. self.proxy_textctrl = self.crt_textctrl()
  516. self.useragent_label = self.crt_statictext("User agent")
  517. self.useragent_textctrl = self.crt_textctrl()
  518. self.referer_label = self.crt_statictext("Referer")
  519. self.referer_textctrl = self.crt_textctrl()
  520. self.logging_label = self.crt_statictext("Logging")
  521. self.enable_log_checkbox = self.crt_checkbox("Enable log", self._on_enable_log)
  522. self.view_log_button = self.crt_button("View", self._on_view)
  523. self.clear_log_button = self.crt_button("Clear", self._on_clear)
  524. self._set_layout()
  525. if self.log_manager is None:
  526. self.view_log_button.Disable()
  527. self.clear_log_button.Disable()
  528. def _set_layout(self):
  529. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  530. vertical_sizer = wx.BoxSizer(wx.VERTICAL)
  531. # Set up retries box
  532. retries_sizer = wx.BoxSizer(wx.HORIZONTAL)
  533. retries_sizer.Add(self.retries_label, flag=wx.ALIGN_CENTER_VERTICAL)
  534. retries_sizer.AddSpacer((20, -1))
  535. retries_sizer.Add(self.retries_spinctrl)
  536. vertical_sizer.Add(retries_sizer, flag=wx.ALIGN_RIGHT | wx.TOP | wx.RIGHT, border=5)
  537. # Set up authentication box
  538. vertical_sizer.Add(self.auth_label, flag=wx.TOP, border=10)
  539. auth_sizer = wx.GridBagSizer(5, -1)
  540. auth_sizer.Add(self.username_label, (0, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  541. auth_sizer.Add(self.username_textctrl, (0, 2))
  542. auth_sizer.Add(self.password_label, (1, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  543. auth_sizer.Add(self.password_textctrl, (1, 2))
  544. auth_sizer.Add(self.video_pass_label, (2, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  545. auth_sizer.Add(self.video_pass_textctrl, (2, 2))
  546. auth_sizer.AddGrowableCol(1)
  547. vertical_sizer.Add(auth_sizer, flag=wx.EXPAND | wx.ALL, border=5)
  548. # Set up network box
  549. vertical_sizer.Add(self.network_label, flag=wx.TOP, border=10)
  550. network_sizer = wx.GridBagSizer(5, -1)
  551. network_sizer.Add(self.proxy_label, (0, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  552. network_sizer.Add(self.proxy_textctrl, (0, 2))
  553. network_sizer.Add(self.useragent_label, (1, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  554. network_sizer.Add(self.useragent_textctrl, (1, 2))
  555. network_sizer.Add(self.referer_label, (2, 0), flag=wx.ALIGN_CENTER_VERTICAL)
  556. network_sizer.Add(self.referer_textctrl, (2, 2))
  557. network_sizer.AddGrowableCol(1)
  558. vertical_sizer.Add(network_sizer, flag=wx.EXPAND | wx.ALL, border=5)
  559. # Set up logging box
  560. vertical_sizer.Add(self.logging_label, flag=wx.TOP, border=10)
  561. logging_sizer = wx.BoxSizer(wx.HORIZONTAL)
  562. logging_sizer.Add(self.enable_log_checkbox, 1)
  563. logging_sizer.Add(self.view_log_button)
  564. logging_sizer.AddSpacer((5, -1))
  565. logging_sizer.Add(self.clear_log_button)
  566. vertical_sizer.Add(logging_sizer, flag=wx.EXPAND | wx.ALL, border=5)
  567. main_sizer.Add(vertical_sizer, 1, wx.EXPAND | wx.ALL, border=5)
  568. self.SetSizer(main_sizer)
  569. def _on_enable_log(self, event):
  570. """Event handler for the wx.EVT_CHECKBOX of the enable_log_checkbox."""
  571. wx.MessageBox("In order for the changes to take effect please restart {0}.".format(__appname__),
  572. "Restart",
  573. wx.OK | wx.ICON_INFORMATION,
  574. self)
  575. def _on_view(self, event):
  576. """Event handler for the wx.EVT_BUTTON of the view_log_button."""
  577. log_window = LogGUI(self)
  578. log_window.load(self.log_manager.log_file)
  579. log_window.Show()
  580. def _on_clear(self, event):
  581. """Event handler for the wx.EVT_BUTTON of the clear_log_button."""
  582. if self.log_manager is not None:
  583. self.log_manager.clear()
  584. def load_options(self):
  585. self.retries_spinctrl.SetValue(self.opt_manager.options["retries"])
  586. self.username_textctrl.SetValue(self.opt_manager.options["username"])
  587. self.password_textctrl.SetValue(self.opt_manager.options["password"])
  588. self.video_pass_textctrl.SetValue(self.opt_manager.options["video_password"])
  589. self.proxy_textctrl.SetValue(self.opt_manager.options["proxy"])
  590. self.useragent_textctrl.SetValue(self.opt_manager.options["user_agent"])
  591. self.referer_textctrl.SetValue(self.opt_manager.options["referer"])
  592. self.enable_log_checkbox.SetValue(self.opt_manager.options["enable_log"])
  593. def save_options(self):
  594. self.opt_manager.options["retries"] = self.retries_spinctrl.GetValue()
  595. self.opt_manager.options["username"] = self.username_textctrl.GetValue()
  596. self.opt_manager.options["password"] = self.password_textctrl.GetValue()
  597. self.opt_manager.options["video_password"] = self.video_pass_textctrl.GetValue()
  598. self.opt_manager.options["proxy"] = self.proxy_textctrl.GetValue()
  599. self.opt_manager.options["user_agent"] = self.useragent_textctrl.GetValue()
  600. self.opt_manager.options["referer"] = self.referer_textctrl.GetValue()
  601. self.opt_manager.options["enable_log"] = self.enable_log_checkbox.GetValue()
  602. class ExtraTab(TabPanel):
  603. def __init__(self, *args, **kwargs):
  604. super(ExtraTab, self).__init__(*args, **kwargs)
  605. self.cmdline_args_label = self.crt_statictext("Command line arguments (e.g. --help)")
  606. self.cmdline_args_textctrl = self.crt_textctrl(wx.TE_MULTILINE | wx.TE_LINEWRAP)
  607. self.extra_opts_label = self.crt_statictext("Extra options")
  608. self.ignore_errors_checkbox = self.crt_checkbox("Ignore errors")
  609. self._set_layout()
  610. def _set_layout(self):
  611. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  612. vertical_sizer = wx.BoxSizer(wx.VERTICAL)
  613. vertical_sizer.Add(self.cmdline_args_label)
  614. vertical_sizer.Add(self.cmdline_args_textctrl, 1, wx.EXPAND | wx.ALL, border=5)
  615. vertical_sizer.Add(self.extra_opts_label, flag=wx.TOP, border=5)
  616. extra_opts_sizer = wx.WrapSizer()
  617. extra_opts_sizer.Add(self.ignore_errors_checkbox)
  618. vertical_sizer.Add(extra_opts_sizer, flag=wx.ALL, border=5)
  619. main_sizer.Add(vertical_sizer, 1, wx.EXPAND | wx.ALL, border=5)
  620. self.SetSizer(main_sizer)
  621. def load_options(self):
  622. self.cmdline_args_textctrl.SetValue(self.opt_manager.options["cmd_args"])
  623. self.ignore_errors_checkbox.SetValue(self.opt_manager.options["ignore_errors"])
  624. def save_options(self):
  625. self.opt_manager.options["cmd_args"] = self.cmdline_args_textctrl.GetValue()
  626. self.opt_manager.options["ignore_errors"] = self.ignore_errors_checkbox.GetValue()
  627. class LogGUI(wx.Frame):
  628. """Simple window for reading the STDERR.
  629. Attributes:
  630. TITLE (string): Frame title.
  631. FRAME_SIZE (tuple): Tuple that holds the frame size (width, height).
  632. Args:
  633. parent (wx.Window): Frame parent.
  634. """
  635. # REFACTOR move it on widgets module
  636. TITLE = _("Log Viewer")
  637. FRAME_SIZE = (650, 200)
  638. def __init__(self, parent=None):
  639. wx.Frame.__init__(self, parent, title=self.TITLE, size=self.FRAME_SIZE)
  640. panel = wx.Panel(self)
  641. self._text_area = wx.TextCtrl(
  642. panel,
  643. style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL
  644. )
  645. sizer = wx.BoxSizer()
  646. sizer.Add(self._text_area, 1, wx.EXPAND)
  647. panel.SetSizerAndFit(sizer)
  648. def load(self, filename):
  649. """Load file content on the text area. """
  650. if os_path_exists(filename):
  651. self._text_area.LoadFile(filename)