Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import os, osproc, parsecfg, streams, strutils, json, winlean
  2. import wox, registry
  3.  
  4. var wp = newWox()
  5.  
  6. proc getSessions(): seq[string] =
  7.  
  8. result = @[]
  9.  
  10. let iniPath = joinPath(getConfigDir(), "winscp.ini")
  11.  
  12. if fileExists(iniPath):
  13. var
  14. cfg = readFile(iniPath)
  15. p: CfgParser
  16.  
  17. open(p, newStringStream(cfg), "")
  18.  
  19. while true:
  20. var e = next(p)
  21. case e.kind
  22. of cfgEof:
  23. break
  24. of cfgSectionStart:
  25. if e.section.startsWith("Sessions"):
  26. result.add(e.section.split("\\")[1])
  27. else:
  28. continue
  29.  
  30. close(p)
  31. else:
  32. var h: RegHandle
  33. try:
  34. h = open("HKEY_CURRENT_USER\\SOFTWARE\\Martin Prikryl\\WinSCP 2\\Sessions", samRead)
  35. for item in h.enumSubkeys:
  36. if not item.startsWith("Default"):
  37. result.add(item)
  38. except:
  39. discard
  40.  
  41. proc getPath(s: string): string =
  42. result = s
  43. result.delete(0, 6)
  44. return result.strip
  45.  
  46. proc addPath(name: string) =
  47. var j = parseJson(
  48. """{"method": "Wox.ChangeQuery","parameters":["scp addpath enter_path_to_winscp", true]}"""
  49. )
  50. echo j
  51.  
  52. proc savePath(path: string) =
  53. writeFile("blabla.txt", "newpath: " & path)
  54. wp.settings["path"] = newJString(expandFilename(path))
  55. wp.saveSettings()
  56. var j = parseJson(
  57. """{"method": "Wox.ChangeQuery","parameters":["scp", true]}"""
  58. )
  59. echo j
  60.  
  61. proc openSession(session: string) =
  62. let
  63. path = wp.settings["path"]
  64. discard startProcess(path.getStr, args = [session])
  65. # discard shellExecuteW(
  66. # 0'i32,
  67. # nil,
  68. # newWideCString(path.getStr),
  69. # newWideCString(session),
  70. # nil,
  71. # 0)
  72.  
  73. proc query(query: string) =
  74.  
  75. if not wp.settings.hasKey("path"):
  76. wp.settings["path"] = newJString(getEnv("programfiles(x86)") / "WinSCP" / "WinSCP2.exe")
  77. wp.saveSettings()
  78.  
  79. if (not fileExists(wp.settings["path"].getStr)) and (not query.startsWith("addpath")):
  80. wp.add(
  81. "Please enter a WinSCP.exe path",
  82. "Can't find WinSCP.exe",
  83. "Images\\winscp.png",
  84. "addPath",
  85. "addpath",
  86. true
  87. )
  88. echo wp.results()
  89. elif query.startsWith("addpath") and query.split.len > 1:
  90. var newPath = getPath(query)
  91. if fileExists(newPath):
  92. wp.add(
  93. "Save path",
  94. "Please enter to save path",
  95. "Images\\winscp.png",
  96. "savePath",
  97. newPath,
  98. true
  99. )
  100. echo wp.results()
  101. else:
  102. wp.add(
  103. "Please enter a valid WinSCP.exe path",
  104. "Can't find WinSCP.exe",
  105. "Images\\winscp.png",
  106. "",
  107. "",
  108. true
  109. )
  110. echo wp.results()
  111. else:
  112. var sessions = getSessions()
  113. for session in sessions:
  114. wp.add(
  115. session,
  116. "Open " & session & " in WinSCP",
  117. "Images\\winscp.png",
  118. "openSession",
  119. session,
  120. false
  121. )
  122. echo wp.results()
  123.  
  124. when isMainModule:
  125. register("query", query)
  126. register("addPath", addPath)
  127. register("savePath", savePath)
  128. register("openSession", openSession)
  129. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement