]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicemenuinstallation
Merge branch 'Applications/19.04'
[dolphin.git] / src / settings / services / servicemenuinstallation
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2009 Jonathan Schmidt-Dominé <devel@the-user.org>
4 # Copyright (C) 2019 Harald Sitter <sitter@kde.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 require 'fileutils'
22
23 ARCHIVE_UNCOMPRESSORS = {
24 'application/x-tar' => :"tar -xf %s -C %s",
25 'application/tar' => :"tar -xf %s -C %s",
26 'application/x-gzip' => :"tar -zxf %s -C %s",
27 'application/gzip' => :"tar -zxf %s -C %s",
28 'application/x-gzip-compressed-tar' => :"tar -zxf %s -C %s",
29 'application/gzip-compressed-tar' => :"tar -zxf %s -C %s",
30 'application/x-gzip-compressed' => :"tar -zxf %s -C %s",
31 'application/gzip-compressed' => :"tar -zxf %s -C %s",
32 'application/bzip' => :"tar -jxf %s -C %s",
33 'application/bzip2' => :"tar -jxf %s -C %s",
34 'application/x-bzip' => :"tar -jxf %s -C %s",
35 'application/x-bzip2' => :"tar -jxf %s -C %s",
36 'application/bzip-compressed' => :"tar -jxf %s -C %s",
37 'application/bzip2-compressed' => :"tar -jxf %s -C %s",
38 'application/x-bzip-compressed' => :"tar -jxf %s -C %s",
39 'application/x-bzip2-compressed' => :"tar -jxf %s -C %s",
40 'application/bzip-compressed-tar' => :"tar -jxf %s -C %s",
41 'application/bzip2-compressed-tar' => :"tar -jxf %s -C %s",
42 'application/x-bzip-compressed-tar' => :"tar -jxf %s -C %s",
43 'application/x-bzip2-compressed-tar' => :"tar -jxf %s -C %s",
44 'application/zip' => :"unzip %s -d %s",
45 'application/x-zip' => :"unzip %s -d %s",
46 'application/x-zip-compressed' => :"unzip %s -d %s",
47 'multipart/x-zip' => :"unzip %s -d %s",
48 'application/tgz' => :"tar -zxf %s -C %s",
49 'application/x-compressed-gtar' => :"tar -zxf %s -C %s",
50 'file/tgz' => :"tar -zxf %s -C %s",
51 'multipart/x-tar-gz' => :"tar -zxf %s -C %s",
52 'application/x-gunzip' => :"tar -zxf %s -C %s",
53 'application/gzipped' => :"tar -zxf %s -C %s",
54 'gzip/document' => :"tar -zxf %s -C %s",
55 'application/x-bz2 ' => :"tar -jxf %s -C %s",
56 'application/x-gtar' => :"tar -xf %s -C %s",
57 'multipart/x-tar' => :"tar -xf %s -C %s"
58 }
59
60 ARCHIVE = ARGV[0]
61
62 # @param log_msg [String] error that gets logged to CLI
63 def fail(log_msg: nil)
64 # FIXME: this is not translated...
65 msg = 'Dolphin service menu installation failed'
66 warn log_msg if log_msg
67 system('kdialog', '--passivepopup', msg, '15')
68 abort
69 end
70
71 def mime_type(filename)
72 ret = `xdg-mime query filetype #{filename}`.strip
73 return ret if $?.success?
74
75 warn 'Failed to xdg-mime'
76 fail(log_msg: "Failed to xdg-mime #{filename}: #{ret}")
77 end
78
79 def uncompress(filename, output)
80 uncompressor = ARCHIVE_UNCOMPRESSORS.fetch(mime_type(filename)).to_s
81 system(format(uncompressor, filename, output))
82 rescue KeyError => e
83 # If a mimetype doesn't have an uncompressor mapped we'll get a keyerror.
84 # we'll log the error but visually report the failure.
85 fail(log_msg: "Unmapped compression format #{filename}; #{e.message}")
86 end
87
88 data_location = `qtpaths --writable-path GenericDataLocation`.strip
89 unless $?.success?
90 fail(log_msg: "Could not get GenericDataLocation #{data_location}")
91 end
92 servicedir = "#{data_location}/kservices5/ServiceMenus/"
93
94 FileUtils.mkdir_p(servicedir) unless File.exist?(servicedir)
95 if ARCHIVE.end_with?('.desktop')
96 puts 'Single-File Service-Menu'
97 puts ARCHIVE
98 puts servicedir
99 FileUtils.cp(ARCHIVE, servicedir)
100 exit
101 end
102
103 dir = "#{ARCHIVE}-dir"
104
105 FileUtils.rm_r(dir) if File.exist?(dir)
106 FileUtils.mkdir(dir)
107
108 fail(log_msg: 'uncompress failed') unless uncompress(ARCHIVE, dir)
109
110 install_it = nil
111 %w[install-it.sh install-it].find do |script|
112 install_it = Dir.glob("#{dir}/**/#{script}")[0]
113 end
114
115 installer = nil
116 %w[installKDE4.sh installKDE4 install.sh install].find do |script|
117 installer = Dir.glob("#{dir}/**/#{script}")[0]
118 end
119
120 Dir.chdir(dir) do
121 installed = false
122
123 [install_it, installer].uniq.compact.each { |f| File.chmod(0o700, f) }
124
125 if install_it
126 puts "[servicemenuinstallation]: Trying to run install_it #{install_it}"
127 installed = system(install_it)
128 elsif installer
129 puts "[servicemenuinstallation]: Trying to run installer #{installer}"
130 %w[--local --local-install --install].any? do |arg|
131 installed = system(installer, arg)
132 end
133 end
134
135 fail unless installed
136 end