• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
UBports Robot Logo UBports Forum
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

SOLVED: How do I get my app to launch the web browser?

Scheduled Pinned Locked Moved App Development
21 Posts 6 Posters 4.0k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      Craig
      last edited by Craig 11 Mar 2020, 22:25

      I'm making apps with C++ and SDL, I've read the UBports documentation but I can't figure out how to launch the web browser from my app. On a desktop system, I use the C++ system() command and can open other programs. Example:

      if ( event.type == SDL_FINGERDOWN ) {
      system("sensible-browser https://forums.ubports.com/");
      }

      It's not working on Ubuntu Touch. I've tried this:
      system("morph-browser --desktop_file_hint=/usr/share/applications/morph-browser.desktop https://forums.ubports.com/");

      Can someone point me to some documentation on how it's done with C++?

      Thank you.

      L 1 Reply Last reply 11 Mar 2020, 23:09 Reply Quote 1
      • L Offline
        lduboeuf @Craig
        last edited by 11 Mar 2020, 23:09

        @Craig https://github.com/ubports/url-dispatcher ?

        C 1 Reply Last reply 12 Mar 2020, 04:36 Reply Quote 0
        • C Offline
          Craig @lduboeuf
          last edited by 12 Mar 2020, 04:36

          @lduboeuf thank you. I downloaded the url-dispatcher package and read many of the files. I imagine I would need to run make on some of these files, put an #include in my existing main.cpp or main.h and then I can call a function that will launch the web browser? Are there any instructions for doing this? Details wont bore me, I need them.

          Thank you.

          1 Reply Last reply Reply Quote 0
          • D Offline
            dobey
            last edited by dobey 3 Dec 2020, 17:48 12 Mar 2020, 04:44

            First off, you should not use system(). If you must exec an external binary, you should use exec() family of calls instead.

            Are you using Qt at all? Or glib/gio?

            If you are using either of these, there are APIs to open URLs with the registered URL handler.

            C 1 Reply Last reply 12 Mar 2020, 04:52 Reply Quote 0
            • C Offline
              Craig @dobey
              last edited by 12 Mar 2020, 04:52

              @dobey Thank you for your reply. Yes, with app confinement, I realized system() was not going to work.

              I'm using pure C++ and SDL in my app. That's it.

              I downloaded the url-dispatcher that @lduboeuf recommended.
              https://github.com/ubports/url-dispatcher

              However, it didn't come with any instructions.

              L D 2 Replies Last reply 12 Mar 2020, 08:52 Reply Quote 0
              • L Offline
                lduboeuf @Craig
                last edited by 12 Mar 2020, 08:52

                @Craig Really not sure of the url-dispatcher thing. Hope i don't gave you wrong instruction πŸ™‚

                1 Reply Last reply Reply Quote 0
                • D Offline
                  dobey @Craig
                  last edited by 12 Mar 2020, 17:48

                  @Craig url-dispatcher is the service which manages URL handlers on UT. It's not something you'd use directly.

                  Try doing something like:

                  #include <unistd.h>
                  
                  ...
                      execlp("xdg-open", MY_URL, NULL);
                  ...
                  

                  If your code is actually C++, I'd suggest using nullptr instead of NULL here, but this should do what you want. And MY_URL would be the URL string you want to open. Also, if using C++11 or newer, you probably would want to do the execlp() call inside a std::thread as well.

                  C 1 Reply Last reply 13 Mar 2020, 04:15 Reply Quote 0
                  • C Offline
                    Craig @dobey
                    last edited by 13 Mar 2020, 04:15

                    @dobey Thank you for your reply! I like your idea. It sounds simple. However, I tried it and it didn't work.

                    This works from adb shell:
                    morph-browser --desktop_file_hint=/usr/share/applications/morph-browser.desktop https://forums.ubports.com/

                    However, from C++, this did not work:
                    #include <unistd.h>
                    ...
                    execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );

                    I also did "int error = execlp()..." and it always returned -1 (error).

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      arubislander
                      last edited by arubislander 13 Mar 2020, 08:54

                      @Craig said in How do I get my app to launch the web browser?:

                      execlp("morph-browser", "--desktop_file_hint=/usr/share/applications/morph-browser.desktop", "https://forums.ubports.com/", nullptr );

                      Have you tried:

                      execlp("xdg-open","https://forums.ubports.com/", nullptr );
                      

                      as per @dobey 's instructions?

                      πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                      Happily running Ubuntu Touch
                      Google Pixel 3a (20.04 DEV)
                      JingPad (24.04 preview)
                      Meizu Pro 5 (16.04 DEV)

                      C 1 Reply Last reply 13 Mar 2020, 09:30 Reply Quote 0
                      • C Offline
                        Craig @arubislander
                        last edited by 13 Mar 2020, 09:30

                        @arubislander Yes, I did try that first. It appears xdg-open isn't installed on Ubuntu Touch. Have you tried it from a C++ app? If you try it from adb shell it says, "bash: xdg-open: command not found".

                        1 Reply Last reply Reply Quote 0
                        • A Offline
                          arubislander
                          last edited by 13 Mar 2020, 09:43

                          I must admit I haven't. Just checked, and indeed xdg-open is not installed. So the question becomes: What are the options for launching the default url handler?

                          πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                          Happily running Ubuntu Touch
                          Google Pixel 3a (20.04 DEV)
                          JingPad (24.04 preview)
                          Meizu Pro 5 (16.04 DEV)

                          1 Reply Last reply Reply Quote 0
                          • A Offline
                            arubislander
                            last edited by 13 Mar 2020, 10:48

                            There is a thread somewhere on this forum concerning launching the browser from QML. I do not have the opportunity to search for it a.t.m. Might do so when I get home, if no one beats me to it. I am thinking something similar must be posible from C++ πŸ€”

                            πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
                            Happily running Ubuntu Touch
                            Google Pixel 3a (20.04 DEV)
                            JingPad (24.04 preview)
                            Meizu Pro 5 (16.04 DEV)

                            1 Reply Last reply Reply Quote 0
                            • A Offline
                              AppLee
                              last edited by 13 Mar 2020, 11:36

                              From QML, I have this working :

                               Action {
                                      iconName: "info"
                                      onTriggered: Qt.openUrlExternally("https://www.domoticz.com/")
                                    }
                              

                              I don't remember what to include to have Qt.openUrlExternally() in your C++ code.
                              Might help...

                              D 1 Reply Last reply 13 Mar 2020, 18:51 Reply Quote 0
                              • D Offline
                                dobey @AppLee
                                last edited by 13 Mar 2020, 18:51

                                @AppLee One would need to use https://doc.qt.io/qt-5.9/qdesktopservices.html#openUrl

                                C 1 Reply Last reply 13 Mar 2020, 20:50 Reply Quote 1
                                • C Offline
                                  Craig @dobey
                                  last edited by 13 Mar 2020, 20:50

                                  @dobey You are right. I was testing QDesktopServices::openUrl when you posted this.

                                  My C++/SDL app launches the morph web browser. Here's how to make a C++/SDL app launch the web browser in Ubuntu Touch:

                                  Step 1:
                                  You need to add these to your CMakeLists.txt:
                                  (You may have more than one CMakeList.txt file and some of the code lines below will go in different CMakeList.txt files.)

                                  (Add these lines below after "project(...)" and "cmake_minimum_required(..)")

                                  find_package(Qt5Core)
                                  find_package(Qt5Qml)
                                  find_package(Qt5Quick)
                                  

                                  (Add this line below after "execute_process(... OUTPUT_VARIABLE ARCH_TRIPLET ...)" )

                                  set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")
                                  

                                  (Add this line below after "add_executable(... main.cpp ...)" )

                                  qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)
                                  

                                  Replace YOURPROJECTNAMEHERE with your project name.

                                  Step 2:
                                  Put these headers near the top of main.h/main.cpp:

                                  #include <QGuiApplication>
                                  #include <QDesktopServices>
                                  #include <QUrl>
                                  

                                  Put this at the top of main():

                                  QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
                                  

                                  Near the end of main(), replace "return 0;" with this:

                                  return app->exec();
                                  

                                  Step 3:
                                  And now for the magic... Somewhere in your C++ code, make your link:

                                  if ( event.type == SDL_FINGERDOWN )
                                  {
                                      QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
                                  }
                                  

                                  If you have any comments, suggestions or ways to improve this code, let me know. Thank you all for your help.

                                  D 1 Reply Last reply 13 Mar 2020, 21:34 Reply Quote 3
                                  • D Offline
                                    dobey @Craig
                                    last edited by 13 Mar 2020, 21:34

                                    @Craig said in How do I get my app to launch the web browser?:

                                    find_package(Qt5Core)
                                    find_package(Qt5Qml)
                                    find_package(Qt5Quick)

                                    You don't need Quick or Qml here. Though I think you will need Gui. Also it'd be better to do:

                                    find_package(Qt5 COMPONENTS Core Gui)
                                    

                                    set(QT_IMPORTS_DIR "lib/${ARCH_TRIPLET}")

                                    You don't need this, as you aren't using any QML parts.

                                    qt5_use_modules(YOURPROJECTNAMEHERE Gui Qml Quick QuickControls2)

                                    For this, you should instead just add

                                    Qt5::Core
                                    Qt5::Gui
                                    

                                    to the list of libraries you pass to the target_link_libraries() call.

                                    I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.

                                    C 1 Reply Last reply 14 Mar 2020, 00:58 Reply Quote 1
                                    • C Offline
                                      Craig @dobey
                                      last edited by Craig 14 Mar 2020, 00:58

                                      @dobey I made the simplifications to the CMakeLists.txt file that you suggested. It's working, thank you!

                                      In the last part of your previous message, you wrote:

                                      I'm also not sure if you need a QApplication thread running at all for this. If possible, it's probably better to avoid having to integrate the Qt main loop and SDL. If it is needed though, it would probably be better to use QCoreApplication or QApplication instead, if possible.

                                      I think you meant something other than QApplication the second time you mentioned it. Which library instead of QApplication?

                                      I tried it with these in main():

                                      QCoreApplication a(argc, args);
                                      return a.exec();
                                      

                                      Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.

                                      D 1 Reply Last reply 14 Mar 2020, 19:00 Reply Quote 0
                                      • D Offline
                                        dobey @Craig
                                        last edited by 14 Mar 2020, 19:00

                                        @Craig said in How do I get my app to launch the web browser?:

                                        Although clickable compiled and it ran, QDesktopServices::openUrl() did not work at all. The browser would not launch.

                                        OK. I suppose you do need to use QGuiApplication then.

                                        1 Reply Last reply Reply Quote 0
                                        • C Offline
                                          Craig
                                          last edited by 20 Mar 2020, 04:56

                                          So, just to recap, here's how to make a C++ app launch the web browser in Ubuntu Touch:

                                          Step 1:
                                          You need to add/change these in your CMakeLists.txt:

                                          find_package(Qt5 COMPONENTS Core Gui)
                                          target_link_libraries(YOURPROJECTNAMEHERE ... Qt5::Core Qt5::Gui)
                                          

                                          Step 2:
                                          Put these headers near the top of main.h/main.cpp:

                                          #include <QGuiApplication>
                                          #include <QDesktopServices>
                                          #include <QUrl>
                                          

                                          Put this at the top of main():

                                          QGuiApplication *app = new QGuiApplication(argc, (char**)argv);
                                          

                                          Near the end of main(), replace "return 0;" with this:

                                          return app->exec();
                                          

                                          Step 3:
                                          Somewhere in your C++ code, when the app user clicks on your button, call this:

                                          QDesktopServices::openUrl(QUrl("https://forums.ubports.com"));
                                          
                                          L 1 Reply Last reply 20 Mar 2020, 09:24 Reply Quote 3
                                          • L Offline
                                            lduboeuf @Craig
                                            last edited by 20 Mar 2020, 09:24

                                            @Craig thanks for sharing πŸ™‚

                                            1 Reply Last reply Reply Quote 0
                                            5 out of 21
                                            • First post
                                              5/21
                                              Last post