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.

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