specialfolders.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # @Base: Miro - an RSS based video player application
  2. # Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
  3. # Participatory Culture Foundation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. #
  19. # In addition, as a special exception, the copyright holders give
  20. # permission to link the code of portions of this program with the OpenSSL
  21. # library.
  22. #
  23. # You must obey the GNU General Public License in all respects for all of
  24. # the code used other than OpenSSL. If you modify file(s) with this
  25. # exception, you may extend this exception to your version of the file(s),
  26. # but you are not obligated to do so. If you do not wish to do so, delete
  27. # this exception statement from your version. If you delete this exception
  28. # statement from all source files in the program, then also delete it here.
  29. """Contains the locations of special windows folders like "My
  30. Documents".
  31. """
  32. import ctypes
  33. import os
  34. # from miro import u3info
  35. GetShortPathName = ctypes.windll.kernel32.GetShortPathNameW
  36. _special_folder_CSIDLs = {
  37. "Fonts": 0x0014,
  38. "AppData": 0x001a,
  39. "My Music": 0x000d,
  40. "My Pictures": 0x0027,
  41. "My Videos": 0x000e,
  42. "My Documents": 0x0005,
  43. "Desktop": 0x0000,
  44. "Common AppData": 0x0023,
  45. "System": 0x0025
  46. }
  47. def get_short_path_name(name):
  48. """Given a path, returns the shortened path name.
  49. """
  50. buf = ctypes.c_wchar_p(name)
  51. buf2 = ctypes.create_unicode_buffer(1024)
  52. if GetShortPathName(buf, buf2, 1024):
  53. return buf2.value
  54. else:
  55. return buf.value
  56. def get_special_folder(name):
  57. """Get the location of a special folder. name should be one of
  58. the following: 'AppData', 'My Music', 'My Pictures', 'My Videos',
  59. 'My Documents', 'Desktop'.
  60. The path to the folder will be returned, or None if the lookup
  61. fails
  62. """
  63. try:
  64. csidl = _special_folder_CSIDLs[name]
  65. except KeyError:
  66. # FIXME - this will silently fail if the dev did a typo
  67. # for the path name. e.g. My Musc
  68. return None
  69. buf = ctypes.create_unicode_buffer(260)
  70. SHGetSpecialFolderPath = ctypes.windll.shell32.SHGetSpecialFolderPathW
  71. if SHGetSpecialFolderPath(None, buf, csidl, False):
  72. return buf.value
  73. else:
  74. return None
  75. common_app_data_directory = get_special_folder("Common AppData")
  76. app_data_directory = get_special_folder("AppData")
  77. base_movies_directory = get_special_folder('My Videos')
  78. non_video_directory = get_special_folder('Desktop')
  79. # The "My Videos" folder isn't guaranteed to be listed. If it isn't
  80. # there, we do this hack.
  81. if base_movies_directory is None:
  82. base_movies_directory = os.path.join(
  83. get_special_folder('My Documents'), 'My Videos')