File: main.lua

package info (click to toggle)
xournalpp 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,036 kB
  • sloc: cpp: 64,137; xml: 939; sh: 752; ansic: 362; python: 338; php: 74; makefile: 15
file content (130 lines) | stat: -rw-r--r-- 4,406 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- The QuickScreenshot plugin:
--
-- Allows the user to save a region to disk as png image.
-- To be used so that users can save various diagrams, etc
-- to their directory of choice; useful when creating a lot
-- of diagrams for presentation.
--
-- TODO:
--  o Support for Windows and macOS is missing. To add support
--    you must edit the functions 'existsUtility' and 'go' to
--    use the right system programs.
--

-- Returns 'true' if the system utility exists;
-- returns 'false' otherwise. This is a dangerous function,
-- since the argument to popen is not sanitized.
-- The programmer must ensure that the argument to
-- existsUtility() is sanitized (not malicious).
local function existsUtility(utility)
  if operatingSystem == "Windows" then
    -- Windows not supported yet.
  elseif operatingSystem == "Darwin" then
    -- macOS not supported yet.
  else
    local f = assert(io.popen("command -v " .. utility))
    local s = assert(f:read('*a'))
    f:close()
    return s ~= ""
  end
end

-- Return one of "Windows", "Linux", "Darwin", "FreeBSD",
-- "OpenBSD" or "NetBSD", etc.
local function findOS()
  if package.config:sub(1,1) == '\\' then
    return "Windows"
  else
    local f = assert(io.popen("uname -s", 'r'))
    local s = assert(f:read('*a'))
    f:close()
    return s
  end
end

-- Register all Toolbar actions and initialize all UI stuff
function initUi()
  app.registerUi({ ["menu"] = "QuickScreenshot", ["callback"] = "Go", ["accelerator"] = "<Shift><Alt>t" });
  OperatingSystem = findOS() -- What's the operating system?
end

-- This is the callback that gets executed when the user
-- activates the plugin via the menu or hotkey.
--
-- This function executes a screenshot utility to capture
-- the region the user selects. Then, it calls 'app.fileDialogSave'
-- to create a "Save As" dialog for the user to choose the
-- filename where the image should reside.
function Go()
  local windowsUtilities = {} -- list of windows programs here
  local macUtilities = {} -- list of macOS programs here
  local elseUtilities = -- list of programs for other systems
                        -- maim and xclip are not listed, because they
                        -- are the default
    { "scrot --overwrite --select --silent "
-- gnome-screenshot behaves oddly (pollutes the window)
-- the line is added here to be indicative of what
-- adding another utility looks like.
--
--  , "gnome-screenshot --area --file="
    }

  -- Different operating systems have different
  -- screenshot utilities, so we check the OS here
  if OperatingSystem == "Windows" then
    app.openDialog("Windows not supported yet.", {"OK"}, "", true)
    return
  end

  if OperatingSystem == "Darwin" then
    app.openDialog("macOS not supported yet.", {"OK"}, "", true)
    return
  end

  -- Skip all the detection of tools in case maim and xclip are
  -- installed. Then a screenshot is made with maim and saved to the
  -- clipboard, skipping all other tools
  if existsUtility("maim") and existsUtility("xclip") then
    os.execute("maim -s | xclip -selection clipboard -t image/png &")
    return
  end
  -- This becomes true if at least one screenshot
  -- utility is available on the system
  local foundUtility = false
  -- Check utility availability and use it
  for i,command in ipairs(elseUtilities) do
    local utilityName = command:match("%S+")
    if existsUtility(utilityName) then
      foundUtility = true
      TmpFilename = os.tmpname() .. ".png" -- global, because needed in Save callback
      -- The file extension is added in order to avoid the giblib error: no image grabbed
      -- see https://stackoverflow.com/questions/26326664/scrot-giblib-error-saving-to-file-failed
      local runCommand = assert(io.popen(command .. TmpFilename .. " &"))
      runCommand:read('*all')
      runCommand:close()
      break
    end
  end
  -- No utility available on the system, inform the user
  if not foundUtility then
    print("Missing screenshot utility.")
    print("Please install one of:")
    for i,command in ipairs(elseUtilities) do
      print(command:match("%S+"))
    end
    return
  else
    -- Launch the "Save As" dialog
    app.fileDialogSave("Save", "Untitled.png")
  end
end

function Save(filename)
  if not filename then
    os.remove(TmpFilename)
    return
  end

  local res, msg = app.glib_rename(TmpFilename, filename)
  if not res then print(msg) end
end