Skip to contentSkip to conversation

ui/cocoa.m: fix help menus

PATCH Superseded Programmingkid 2 participants 2 messages Mar 7, 2016 – Mar 15, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ui/cocoa.m: fix help menus

Make the help menus actually work. The code will search thru three different locations for the help file. If it can't be found, it will look on the web for it.

---
Moved opening code to one method.
Searches three different locations on the user's computer first. Attempts to open file on web if file can't be found on user's computer.

 ui/cocoa.m | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
ui/cocoa.m +33 -4
index 3ee5549..66f0f79 100644
@@ -858,6 +858,7 @@ QemuCocoaView *cocoaView;
 - (void)ejectDeviceMedia:(id)sender;
 - (void)changeDeviceMedia:(id)sender;
 - (BOOL)verifyQuit;
+- (void)openDocumentation:(NSString *)filename;
 @end
 
 @implementation QemuCocoaAppController
@@ -994,20 +995,48 @@ QemuCocoaView *cocoaView;
     [cocoaView toggleFullScreen:sender];
 }
 
+/* Tries to find then open the specified filename */
+- (void) openDocumentation: (NSString *) filename
+{
+    /* Where to look for local files */
+    NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
+    NSString *full_file_path;
+    const int number_of_paths = 3;
+
+    /* iterate thru the possible paths until the file is found */
+    int index;
+    for (index = 0; index < number_of_paths; index++) {
+        full_file_path = [[NSBundle mainBundle] executablePath];
+        full_file_path = [full_file_path stringByDeletingLastPathComponent];
+        full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
+                          path_array[index], filename];
+        if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
+            return;
+        }
+    }
+
+    /* Try to open the file on the web if possible */
+    full_file_path = [NSString stringWithFormat: @"%s%@",
+                      "http://qemu.weilnetz.de/", filename];
+    NSURL *url = [NSURL URLWithString: full_file_path];
+    if ([[NSWorkspace sharedWorkspace] openURL: url] == NO) {
+        NSBeep();
+        QEMU_Alert(@"Failed to open file");
+    }
+}
+
 - (void)showQEMUDoc:(id)sender
 {
     COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
 
-    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
-        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
+    [self openDocumentation: @"qemu-doc.html"];
 }
 
 - (void)showQEMUTec:(id)sender
 {
     COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
 
-    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
-        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
+    [self openDocumentation: @"qemu-tech.html"];
 }
 
 /* Stretches video to fit host monitor size */
Signature · 1 line
2.7.2
#1 lore raw
Quoted 27 lines · Programmingkid

Make the help menus actually work. The code will search thru three different locations for the help file. If it can't be found, it will look on the web for it.

---
Moved opening code to one method.
Searches three different locations on the user's computer first. Attempts to open file on web if file can't be found on user's computer.

 ui/cocoa.m | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
ui/cocoa.m +1 -0
index 3ee5549..66f0f79 100644
@@ -858,6 +858,7 @@ QemuCocoaView *cocoaView;
 - (void)ejectDeviceMedia:(id)sender;
 - (void)changeDeviceMedia:(id)sender;
 - (BOOL)verifyQuit;
+- (void)openDocumentation:(NSString *)filename;
 @end
 
 @implementation QemuCocoaAppController

ui/cocoa.m QemuCocoaView *cocoaView; · line 1004

Quoted 10 lines
ui/cocoa.m +7 -0
@@ -994,20 +995,48 @@ QemuCocoaView *cocoaView;
     [cocoaView toggleFullScreen:sender];
 }
 
+/* Tries to find then open the specified filename */
+- (void) openDocumentation: (NSString *) filename
+{
+    /* Where to look for local files */
+    NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
+    NSString *full_file_path;
+    const int number_of_paths = 3;

You should be able to use ARRAY_SIZE(path_array) here, you don't need to hard code the 3.

Quoted 6 lines
ui/cocoa.m +6 -0
+
+    /* iterate thru the possible paths until the file is found */
+    int index;
+    for (index = 0; index < number_of_paths; index++) {
+        full_file_path = [[NSBundle mainBundle] executablePath];
+        full_file_path = [full_file_path stringByDeletingLastPathComponent];

ui/cocoa.m QemuCocoaView *cocoaView; · line 1020

Quoted 10 lines
ui/cocoa.m +10 -0
+        full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
+                          path_array[index], filename];
+        if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
+            return;
+        }
+    }
+
+    /* Try to open the file on the web if possible */
+    full_file_path = [NSString stringWithFormat: @"%s%@",
+                      "http://qemu.weilnetz.de/", filename];

None of our other UI frontends try to look up the docs on the web. If we do want to do this we should be using a qemu-project.org URL, ie one we control directly as a project. Plus there's no versioning here so it will show the docs for the latest version even if you're running an old QEMU.

Adding a URL lookup should be a different patch if you want to do it, but I don't think it is worth the effort of maintaining versioned documentation on the web for all future QEMU releases, when the local files should be right there anyway.

Quoted 16 lines
ui/cocoa.m +8 -2
+    NSURL *url = [NSURL URLWithString: full_file_path];
+    if ([[NSWorkspace sharedWorkspace] openURL: url] == NO) {
+        NSBeep();
+        QEMU_Alert(@"Failed to open file");
+    }
+}
+
 - (void)showQEMUDoc:(id)sender
 {
     COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
 
-    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
-        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
+    [self openDocumentation: @"qemu-doc.html"];
 }
 

ui/cocoa.m QemuCocoaView *cocoaView; · line 1042

Quoted 10 lines
ui/cocoa.m +1 -2
 - (void)showQEMUTec:(id)sender
 {
     COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
 
-    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
-        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
+    [self openDocumentation: @"qemu-tech.html"];
 }
 
 /* Stretches video to fit host monitor size */

thanks
-- PMM