mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 13:12:46 +00:00 
			
		
		
		
	AppFile: Add spawn_with_escalation
This commit is contained in:
		
							parent
							
								
									83f1775f15
								
							
						
					
					
						commit
						c4abb367a4
					
				
					 2 changed files with 42 additions and 0 deletions
				
			
		|  | @ -11,8 +11,10 @@ | |||
| #include <LibCore/ConfigFile.h> | ||||
| #include <LibCore/DirIterator.h> | ||||
| #include <LibCore/Process.h> | ||||
| #include <LibCore/StandardPaths.h> | ||||
| #include <LibDesktop/AppFile.h> | ||||
| #include <LibFileSystem/FileSystem.h> | ||||
| #include <LibGUI/MessageBox.h> | ||||
| 
 | ||||
| namespace Desktop { | ||||
| 
 | ||||
|  | @ -179,4 +181,41 @@ bool AppFile::spawn(ReadonlySpan<StringView> arguments) const | |||
|     return true; | ||||
| } | ||||
| 
 | ||||
| bool AppFile::spawn_with_escalation(ReadonlySpan<StringView> user_arguments) const | ||||
| { | ||||
|     if (!is_valid()) | ||||
|         return false; | ||||
| 
 | ||||
|     StringView exe; | ||||
|     Vector<StringView, 2> args; | ||||
|     // FIXME: These single quotes won't be enough for executables with single quotes in their name.
 | ||||
|     auto pls_with_executable = ByteString::formatted("/bin/pls '{}'", executable()); | ||||
|     if (run_in_terminal() && !requires_root()) { | ||||
|         exe = "/bin/Terminal"sv; | ||||
|         args = { "-e"sv, executable().view() }; | ||||
|     } else if (!run_in_terminal() && requires_root()) { | ||||
|         exe = "/bin/Escalator"sv; | ||||
|         args = { executable().view() }; | ||||
|     } else if (run_in_terminal() && requires_root()) { | ||||
|         exe = "/bin/Terminal"sv; | ||||
|         args = { "-e"sv, pls_with_executable.view() }; | ||||
|     } else { | ||||
|         exe = executable().view(); | ||||
|     } | ||||
|     args.extend(Vector(user_arguments)); | ||||
| 
 | ||||
|     auto pid = Core::Process::spawn(exe, args.span(), | ||||
|         working_directory().is_empty() ? Core::StandardPaths::home_directory() : working_directory()); | ||||
|     if (pid.is_error()) | ||||
|         return false; | ||||
| 
 | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| void AppFile::spawn_with_escalation_or_show_error(GUI::Window& window, ReadonlySpan<StringView> arguments) const | ||||
| { | ||||
|     if (!spawn_with_escalation(arguments)) | ||||
|         GUI::MessageBox::show_error(&window, ByteString::formatted("Failed to spawn {} with escalation", executable())); | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Hugh Davenport
						Hugh Davenport